Algebraic data types

thedeemon via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 6 22:30:40 PDT 2014


On Monday, 6 October 2014 at 16:48:32 UTC, Jonathan wrote:
> Resurrecting this topic, does D have ADTs yet for enums/structs?


Here's a proof of concept:
http://www.infognition.com/blog/2014/recursive_algebraic_types_in_d.html

struct Const(T) { ... }
class Add(T) { ... }

alias Exp = Either!(Add, Const);

int eval(Exp!int e) {
   return e.match(a => a.l + a.r,
                  i => i.x);
}

string show(Exp!string e) {
     return e.match(a => "(" ~ a.l ~ " + " ~ a.r ~ ")",
                    i => i.x.text);
}


More information about the Digitalmars-d mailing list