Algebraic Data Types in D?

Justin Whear via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 31 08:03:09 PDT 2014


On Thu, 31 Jul 2014 11:42:20 +0000, Remo wrote:

> http://tech.esper.com/2014/07/30/algebraic-data-types/
> 
> D already has product type it is struct.
> But D lacks sum type also called tagged-union.
> 
> Do you think it would be possible to add something like this to D2 ?

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.


More information about the Digitalmars-d mailing list