First Draft: Nominal Sum Types via `enum union` and `switch` Expressions
Dukc
ajieskola at gmail.com
Wed Sep 23 09:42:12 UTC 2026
On Wednesday, 23 September 2026 at 09:14:43 UTC, Dukc wrote:
>> I'm fine with that. If you think it's worthwhile, you will
>> need to show me a very convincing example of why it's useful.
>
> ```d
> enum union DynamicNumber
> { case long, case float, case double, case error(string);
> }
>
> @safe sum(DynamicNumber a, DunamicNumber b) => switch(a)
> { case error(s) => a,
> case numA => switch(b)
> { case error(s) => b,
> case numB => numA + numB,
> },
> };
> ```
Actually, even better, assuming tuple pattern matching will one
day work with switch expressions:
```d
@safe sum(DynamicNumber a, DynamicNumber b) => switch(tuple(a, b))
{ case (error(s), _) => a,
case (_, error(s)) => b,
case (float aNum, float bNum) => float(aNum+bNum),
case (aNum, bNum) => aNum+bNum,
};
```
More information about the dip.development
mailing list