www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template mixins has no effect

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I'm trying to improve


through use of template mixins


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?
May 04 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 What is wrong?
For some reason the mixin declarations for op* are not imported into the calling scope when others are. Compiler bug?
May 04 2014
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 What is wrong?
Ping, anybody?
May 22 2014