First Draft: Nominal Sum Types via `enum union` and `switch` Expressions
Meta
jared771 at gmail.com
Tue Sep 15 03:51:43 UTC 2026
On Tuesday, 15 September 2026 at 02:23:01 UTC, Paul Backus wrote:
> On Monday, 14 September 2026 at 19:43:22 UTC, Meta wrote:
>> This is in comparison to std.sumtype where you have to do:
>> ```d
>> struct Some(T) { T val; }
>> alias None = typeof(null);
>> alias Option(T) = SumType!(Some!T, None);
>> Option!ConfigValue getConfigVal(string name)
>> {
>> ...
>> if (...)
>> return Option!ConfigValue(Some!ConfigValue(*val));
>>
>> return Option!ConfigValue.None();
>> }
>>
>> Which gets very tedious.
>
> The way you make this look nice is by defining factory
> functions:
>
> ```d
> struct Some(T) { T val; }
> struct None {} // avoid typeof(null)'s implicit conversions
> alias Option(T) = SumType!(None, Some!T);
>
> Option!T some(T)(T val) => Option!T(Some!T(val));
> enum none(T) = Option!T(None());
>
> Option!ConfigValue getConfigVal(string name)
> {
> ...
> if (...)
> return some(*val);
> return none!ConfigValue;
> }
> ```
Sure, but the one thing library implementations can't do is
implicit conversions. Also the ergonomics are a little better
because you don't have to define a bunch of ancillary types and
factory functions.
More information about the dip.development
mailing list