draft proposal for Sum Types for D

jmh530 john.michael.hall at gmail.com
Tue Nov 29 15:12:28 UTC 2022


On Tuesday, 29 November 2022 at 14:45:09 UTC, Timon Gehr wrote:
> On 11/29/22 07:26, Walter Bright wrote:
>> Go ahead, Make My Day! Destroy!
>> 
>> https://github.com/WalterBright/DIPs/blob/sumtypes/DIPs/1NNN-(wgb).md
>
>
> Some things that are missing:
>
> - non-default construction, e.g.:
>
> sumtype Result{
>     error,
>     int value;
> }
>
> auto result1 = Result.error;
> auto result2 = Result.value(2);
>
> The above is a bit unergonomic, maybe there is a better way to 
> expose those constructors. They should exist though, otherwise 
> it is e.g., impossible to initialize an immutable sumtype.
> [snip]

I was a little confused because the DIP proposal has the same 
example (though using `Error` instead of `error`) and it didn't 
have an identifier with it. According to the syntax, a field 
declaration has to have an identifier with the type. So the only 
other option is `Error`/`error` is an enum. If that's the case, 
it's a little confusing because `Error` is a commonly used class 
already.

Am I right that if you wanted to do the same as below

```d
import std.sumtype;
struct Fahrenheit { double degrees; }
struct Celsius { double degrees; }
struct Kelvin { double degrees; }

alias Temperature = SumType!(Fahrenheit, Celsius, Kelvin);
```

then the equivalent with this DIP would be

```d
sumtype Temperature
{
     Fahrenheit fahrenheit,
     Celsius celsius,
     Kelvin kelvin
}
```


More information about the Digitalmars-d mailing list