[Discussion] Usefulness of Algebraic?

maik klein via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 13 07:50:35 PST 2016


struct Some(T){
     T value;
}
struct None{}

struct Option(T){
     alias OptionAdt(T) = Algebraic!(Some!T, None);
     OptionAdt!T value; //Should default to none
     //OptionAdt!T value = OptionAdt!T(None());
     /*
       Error: memcpy cannot be interpreted at compile time, 
because it has no
       available source code
     */
}

It is a bit unfortunate that the default value of Option!T.init 
is neither Some!T nor None.

Also Algebraic probably has to be wrapped in a struct because D 
can not infer the type of T for the following code.

alias Option(T) = Algebraic!(Some!T, None);

bool test(T)(auto ref Option!T o){
     return true;
}

I really love ADT's but I think that they can not be used as a 
default value limits their usefulness.

/discuss





More information about the Digitalmars-d mailing list