Custom Type Unions

Dennis dkorpel at gmail.com
Sun May 26 18:14:36 UTC 2019


On Sunday, 26 May 2019 at 17:59:21 UTC, Dhruv Singal wrote:
> I'm having trouble figuring out if dlang has a way for me to 
> create a union object that can be one of many object and have 
> it be a first-class object, ie it can be the return type of 
> functions. Is this something dlang supports?

Structs and unions can be returned in D. You can implement a 
tagged union [1] yourself with them, or use an existing library 
solution. The standard library has std.variant [2], but I'd 
recommend the sumtype package [3] (see the 'Features' section in 
the package description).

[1] https://en.wikipedia.org/wiki/Tagged_union
[2] https://dlang.org/phobos/std_variant.html
[3] http://code.dlang.org/packages/sumtype


More information about the Digitalmars-d mailing list