draft proposal for Sum Types for D

Andrey Zherikov andrey.zherikov at gmail.com
Wed Nov 30 08:49:00 UTC 2022


On Tuesday, 29 November 2022 at 06:26:20 UTC, Walter Bright wrote:
> Go ahead, Make My Day! Destroy!
>
> https://github.com/WalterBright/DIPs/blob/sumtypes/DIPs/1NNN-(wgb).md

> cannot produce compile time error if not all the arms are 
> accounted for in a pattern match rather than a thrown exception

It does:
```d
SumType!(int, string) s;
s.match!((int i) => true);  // Error: static assert:  "No 
matching handler for types `(string)`"
```

> `if (?x.busy)`

Please consider moving `?` to be after identifier so the 
following can be allowed:
```d
sumtype C
{
     Default,
     int value
}
sumtype B
{
     Default,
     C c
}
sumtype A
{
     Default,
     B b
}

A a;
if(a.b?.c?.value?)
   ...
```

Compare with `if(?a.b && ?a.b.c && ?a.b.c.value)`

This will also allow natural improving of ternary expression: 
`a.b?:123` === `(a.b?) ? (a.b) : (123)`. (may be just `a.b ? 
123`?)

Also if you even think about adding optional type, `?`-suffix 
will perfectly match here:
```d
int? optional;
if(optional?)
   writeln("it's set to ", optional);

string? s;
writeln("the value is ", s?:"not set");
```


More information about the Digitalmars-d mailing list