draft proposal for Sum Types for D

H. S. Teoh hsteoh at qfbox.info
Sat Dec 3 17:50:19 UTC 2022


On Sat, Dec 03, 2022 at 05:26:41PM +0000, Basile B. via Digitalmars-d wrote:
[...]
> ```d
> // let me explain the problem
> // you want optional access
> 
> struct S
> {
>     int a;
> }
> 
> // so you cant get optional access to `a`
> // because "int" does not allows this so
> // you write
> 
> struct S
> {
>     SomeSumtype a;
> }
> 
> // nice but it's not anymore compatible with C
> // the optional value is part of the struct size
> // you see the problem ?
> //
> // your struct size is not anymore 4 but 8.
> //
> // Optional access must be built in the language as an operator.
> ```
> 
> you see what I mean ?

I don't follow.  Whether or not the sum type is built into the language,
the tag has to be stored *somewhere*, your struct size will not be
compatible with C regardless.  Just because .a uses a built-in sumtype
does not make the tag disappear into thin air, it will still exist in
the struct and occupy space, and the result is still not compatible with
C because C does not understand sumtypes.

Either way, on the C side you have to declare the tag explicitly and
deal with it explicitly, regardless of whether sumtypes are built into
the language or not.


T

-- 
"I'm not childish; I'm just in touch with the child within!" - RL


More information about the Digitalmars-d mailing list