Merge pull request #10665 from pbackus/fix-gh-10540

sumtype: rename 'degrees' to 'value' in example
This commit is contained in:
Paul Backus 2025-03-11 11:07:14 -04:00 committed by GitHub
commit a7e90e9bf3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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)