Template mixins has no effect
"Nordlöw" via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun May 4 06:52:35 PDT 2014
I'm trying to improve
https://github.com/nordlow/justd/blob/master/enums.d
through use of template mixins
http://dlang.org/template-mixin.html
to improve EnumUnion as follows:
struct EnumUnion(E...)
{
alias OriginalType = CommonOriginalType!E;
alias U = UnionEnum!(E); // Wrapped Type.
alias _value this;
mixin template genOpAssign(uint i)
{
@safe pure nothrow void opAssign(E[i] e)
{
_value = cast(U)e;
}
}
mixin template genOpCast(uint i)
{
@safe pure nothrow E[i] opCast(T : E[i])() const @safe
pure nothrow {
bool match = false;
foreach (m; EnumMembers!(E[i])) {
if (m == _value) {
match = true;
}
}
if (!match)
throw new RangeError();
return cast(E[i])_value;
}
}
/* TODO: Alternative to this set of static if? */
static if (E.length >= 1) { mixin genOpAssign!0; mixin
genOpCast!0; }
static if (E.length >= 2) { mixin genOpAssign!1; mixin
genOpCast!1; }
static if (E.length >= 3) { mixin genOpAssign!2; mixin
genOpCast!2; }
private U _value; // Instance.
}
but it doesn't see to have an effect on the conversion rules:
enums.d(162,10): Error: cannot implicitly convert expression (e0)
of type E0 to EnumUnion!(E0, E1, E2)
What is wrong?
More information about the Digitalmars-d-learn
mailing list