First Draft: Nominal Sum Types via `enum union` and `switch` Expressions

Paul Backus snarwin at gmail.com
Tue Sep 15 02:23:01 UTC 2026


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;
}
```


More information about the dip.development mailing list