Algebraic Data Types in D?

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 31 08:58:52 PDT 2014


Justin Whear:

> In addition to the suggestions of Algebraic or Variant 
> elsewhere in this
> thread, it's trivial to implement your own concrete tagged 
> unions:
>
> struct MyTaggedUnion
> {
>    enum Type { Int, Float, String }
>    Type tag;
>
>    union {
>       int int_;
>       float float_;
>       string string_;
>    }
> }
>
> You can also hide the union members with private and only allow 
> access
> via property getters that check the tag.

See for a bare-bones built-in pattern matching, with an optional 
"opMatch" struct method that gets called by the improved switch:
https://d.puremagic.com/issues/show_bug.cgi?id=596

Bye,
bearophile


More information about the Digitalmars-d mailing list