Sum Type by Struct

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Sep 13 11:30:51 UTC 2024


On 13/09/2024 11:16 PM, Nick Treleaven wrote:
> On Friday, 13 September 2024 at 09:26:16 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> 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
> ```

I did this specifically for value type exceptions:

```d
void func() @throws(:FailToDecodeUTF);
```

Would actually be:

```d
size_t func();
```

It just so happens, that this is also a useful codegen trait for sumtypes.

If you rely on structs, you would have to solve the size zero problem 
and that isn't pretty.

https://github.com/rikkimax/DIPs/blob/2a80adb38ac94dd38eac61505fc3c3810b9eae10/DIPs/DIP1xxx-RC.md


More information about the dip.ideas mailing list