[Discussion] Usefulness of Algebraic?

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 13 10:06:25 PST 2016


For reference, the TaggedAlgebraic struct that I wrote some time ago 
supports this: https://github.com/s-ludwig/taggedalgebraic

You could do something similar to this:

struct Option(T) {
   struct Kinds(T) {
     typeof(null) none;
     T some;
   }
   TaggedAlgebraic!Kinds data;
   alias data this;
   @property bool hasValue() const
     { return data.kind == TaggedAlgebraic!Kinds.Kind.some; }
}

void test()
{
   Option!int opt;
   assert(!opt.hasValue);
   opt = 12;
   assert(opt.hasValue);
   assert(opt == 12);
}



More information about the Digitalmars-d mailing list