std.sumtype?

Paul Backus snarwin at gmail.com
Thu Mar 18 18:09:38 UTC 2021


On Thursday, 18 March 2021 at 17:24:13 UTC, Oleg B wrote:
> May be depending on order of types must be fixed too
>
> ```
> alias S1 = Sumtype!(int, float);
> alias S2 = Sumtype!(float, int);
>
> static assert (is(S1 == S2)); // false by now
> ```
> because no different between S1 and S2 in practice use

union U1 { int n; float f; }
union U2 { float f; int n; }

static assert(is(U1 == U2)); // fails

D is a nominally-typed language, not a structurally-typed one. 
That means types that are structurally identical (like the unions 
and SumTypes in the above examples) are still considered separate 
types if their names are spelled differently.

IMO the behavior of SumType in this regard is perfectly 
consistent with the rest of the language. If you want to use 
structural typing instead of nominal typing in your own code, you 
can define template predicates to check for SumTypes with a 
particular structure, the same way Phobos defines predicates like 
`isInputRange` to check for types with a particular structure.


More information about the Digitalmars-d mailing list