draft proposal for Sum Types for D

jmh530 john.michael.hall at gmail.com
Tue Nov 29 16:19:10 UTC 2022


On Tuesday, 29 November 2022 at 13:39:19 UTC, jmh530 wrote:
> 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
>
> The query expression is kind of similar to safe navigation 
> operators [1], but returns true/false instead of the value. I 
> like it, but I wonder if it is something that might be useful 
> more generally.
>
> [1] https://en.wikipedia.org/wiki/Safe_navigation_operator

Thinking on this a little more...reading a member that is not the 
value that is in it results in a runtime error. From this 
perspective, the query expression could be used more generally to 
detect if something would be an error and to return true or false 
instead.

So something like below
```d
import std.stdio: writeln;

void main() {
     int* x = null;
     writeln(*x); //kills program
     writeln(?*x ? *x : "error");
}
```

Maybe it makes more sense to just set ?null equal to false and 
true for any basic types (with non-null values) with some 
similar, consistent rule for aggregate types. This would avoid 
special casing behavior for sumtypes.

I suppose in the back of my head, I am thinking about a situation 
where the user has a sumtype in their code and wants to change it 
to a struct (maybe they realize they only are using one of the 
members). The query expression would no longer work with the 
struct so they would need to go through their code to remove it. 
They can't just change the sumtype to a struct.


More information about the Digitalmars-d mailing list