sumtype: rename 'degrees' to 'value' in example

Strictly speaking, the Kelvin scale is not measured in degrees, so the
previous name was incorrect. Changing it in all cases preserves
consistency.

Fixes #10540
This commit is contained in:
Paul Backus 2025-03-11 10:33:32 -04:00
parent f243f645ad
commit 613ec088e4

View file

@ -32,9 +32,9 @@ version (D_BetterC) {} else
{ {
import std.math.operations : isClose; import std.math.operations : isClose;
struct Fahrenheit { double degrees; } struct Fahrenheit { double value; }
struct Celsius { double degrees; } struct Celsius { double value; }
struct Kelvin { double degrees; } struct Kelvin { double value; }
alias Temperature = SumType!(Fahrenheit, Celsius, Kelvin); alias Temperature = SumType!(Fahrenheit, Celsius, Kelvin);
@ -48,29 +48,29 @@ version (D_BetterC) {} else
{ {
return Fahrenheit( return Fahrenheit(
t.match!( t.match!(
(Fahrenheit f) => f.degrees, (Fahrenheit f) => f.value,
(Celsius c) => c.degrees * 9.0/5 + 32, (Celsius c) => c.value * 9.0/5 + 32,
(Kelvin k) => k.degrees * 9.0/5 - 459.4 (Kelvin k) => k.value * 9.0/5 - 459.4
) )
); );
} }
assert(toFahrenheit(t1).degrees.isClose(98.6)); assert(toFahrenheit(t1).value.isClose(98.6));
assert(toFahrenheit(t2).degrees.isClose(212)); assert(toFahrenheit(t2).value.isClose(212));
assert(toFahrenheit(t3).degrees.isClose(32)); assert(toFahrenheit(t3).value.isClose(32));
// Use ref to modify the value in place. // Use ref to modify the value in place.
void freeze(ref Temperature t) void freeze(ref Temperature t)
{ {
t.match!( t.match!(
(ref Fahrenheit f) => f.degrees = 32, (ref Fahrenheit f) => f.value = 32,
(ref Celsius c) => c.degrees = 0, (ref Celsius c) => c.value = 0,
(ref Kelvin k) => k.degrees = 273 (ref Kelvin k) => k.value = 273
); );
} }
freeze(t1); freeze(t1);
assert(toFahrenheit(t1).degrees.isClose(32)); assert(toFahrenheit(t1).value.isClose(32));
// Use a catch-all handler to give a default result. // Use a catch-all handler to give a default result.
bool isFahrenheit(Temperature t) bool isFahrenheit(Temperature t)