Sum Type by Struct

Nick Treleaven nick at geany.org
Fri Sep 13 11:16:15 UTC 2024


On Friday, 13 September 2024 at 09:26:16 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> As for your approach to aliasing null to create a non-unique 
> type, there is only so many sentinels in the language you can 
> do that for and the type of a null is itself a valid type to be 
> in a sumtype. The closest thing to what you are doing there is 
> ``void`` and there is only one of them.

Just to demonstrate this, if `void` variables were allowed:
```d
void a, b;
b = a; // OK
:X x;
:Y y = x; // error, can't implicitly convert :X to :Y
```

> The type of a member of operator has a size zero, this allows 
> eliding of the value if all of them are size zero. The type of 
> a null is the same as a pointer, after all its a pointer.

That's great. Otherwise you would have to define empty struct 
types and they would probably waste one byte of space:

```d
struct A {}
struct B {}
struct C {}

sumtype S = A | B | C;

A a; // size 1
:X x; // size 0
```


More information about the dip.ideas mailing list