Sum Types - first draft

Dennis dkorpel at gmail.com
Tue Sep 10 10:48:42 UTC 2024


On Tuesday, 10 September 2024 at 04:06:16 UTC, Walter Bright 
wrote:
> https://github.com/WalterBright/documents/blob/96bca2f9f3520cf53ed5c4dec8e5e2d855e64e66/sumtype.md

The evaluation of std.sumtype needs adjustment, because it's full 
of wrong assumptions.

> - std.sumtype cannot optimize the tag out of existence

It could add that special case if it wanted.

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

It can and does if you use `match` instead of `tryMatch`:

```D
SumType!(string, float) s;
s.match!((float x) => 1);
// std/sumtype.d(2018): Error: static assert:  "No matching 
handler for types `(string)`"
```

> - an int and a pointer cannot both be in a sumtype and be safe

It can and is, that's why DIP1035 added `@system` variables.
```D
void main() @safe
{
     SumType!(int, int*) s = new int;
}
```



More information about the dip.development mailing list