Tagged unions [Was: What's wrong with std.variant.Variant?]

Paul Backus snarwin at gmail.com
Mon Jun 15 12:41:03 UTC 2020


On Monday, 15 June 2020 at 04:08:14 UTC, Dukc wrote:
>
> Supporting all attributes definitely sounds good. OTOH your 
> example isn't as DRY as with Taggedalgebraic:
>
> ```
> alias PictureSize = TaggedUnion!PictureSize_;
> union PictureSize_
> {   int[2] inPixels;
>     float[2] inMeters;
>     float[2] inSourceSizes;
> }
> ```
>
> In essence, the problem is that I want an union with a 
> customizable tag, not an afterwards defined "base class" for 
> the types. I'd check Atila's library first for the latter. But 
> I definitely could adapt `SumType` for my use with some extra 
> wrapping.

Personally I would say it is more DRY, because there's no need to 
declare a superfluous `union` type for your tagged version to 
"inherit" from. But really this is just a matter of preference.

I'm not sure what you mean by 'base class.' Are you referring to 
the lack of built-in support for named members? It would not be 
terribly difficult to add a syntax for named SumType members 
similar to std.typecons.Tuple, so that you could write

SumType!(
     int[2], "InPixels",
     float[2], "InMeters",
     float[2], "InSourceSizes"
)

...and have SumType take care of all the `Typedef` stuff for you. 
So far I've held off on it because I figured it was easy enough 
already to do by hand, but if this is a feature that a lot of 
people want, I can certainly add it.


More information about the Digitalmars-d mailing list