www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler bug? Mixin behavior order dependent

reply Norbert Nemec <Norbert Nemec-online.de> writes:
Hi there,

can anyone give an explanation for the following or is it simply a 
compiler bug?

The code below does not compile. I don't see why the enum cannot be 
forward referenced across mixin boundaries while the other two forward 
references are no problem. (Ordering the mixing statement below the enum 
definition also solves the problem)

=====================
mixin template verifyArray() {
    static assert(is(type));  // no problem
    static assert(rank >= 0); // Error: forward reference
}

struct MyArray(T,int R) {
    mixin verifyArray;
    static assert(rank >= 0); // no problem

    alias T type;
    enum rank = R;
}

unittest {
    MyArray!(int,4) x;
}
=====================

./bug_mixin_order.d(5): Error: forward reference of variable rank
./bug_mixin_order.d(9): Error: mixin 
MyArray(int,4).MyArray.verifyArray!() error instantiating 
./bug_mixin_order.d(16): Error: template instance MyArray!(int,4) error 
instantiating

=====================

Any ideas?

Greetings,
Norbert
Oct 16 2011
parent reply Trass3r <un known.com> writes:
Well it's probably a bug if type is recognized but not rank.
But in general it's really bad code style to use external stuff in a mixin.
Oct 16 2011
parent Norbert Nemec <Norbert Nemec-online.de> writes:
On 16.10.2011 18:49, Trass3r wrote:
 Well it's probably a bug if type is recognized but not rank.
 But in general it's really bad code style to use external stuff in a mixin.

True, but it is also quite powerful if used with care. After all the whole point of "mixin template" is to be able to access the surrounding namespace.
Oct 16 2011