On Thursday, 11 July 2013 at 12:30:17 UTC, bearophile wrote:
Algebraic has several problems, but your code has other
problems.
Oh, no doubt. This isn't meant to be serious, industrial strength
code.
I suggest to take a look at Nullable, especially the version
that makes a constant value the "null".
Nullable will work in this case, but it doesn't solve the general
problem with Algebraic.
Also try to almost never use cast() in your code, unless you
_really_ know what you are doing.
I'm well aware of the dangers of cast. I was just playing around,
trying to get this to work.
Expecting a bit of magic from the D compiler is OK. But someone
has to ask for it in a clean way, someone has to implement it,
and someone else has to accept it.
Now that I think about it, I'm wondering exactly why the subtypes
of Algebraic are not covariant with it when returned from a
function. It works fine with other types that use alias this,
e.g.:
import std.variant;
struct Test1
{
}
struct Test2
{
Test1 t;
alias t this;
}
Test1 covarReturn()
{
//Fine
return Test2();
}