Anonymous nested class of problems

Tomek Sowiński just at ask.me
Sat Dec 12 04:09:49 PST 2009


I was playing with what makes the Javists proud, but stumbled over this:

class M {
     this(byte a) { _a = a; }
     byte _a;
     byte a() { return _a; };

     static m = new class(4) M {
         this(byte a) { super(a); }
         override byte a() { return 3+_a; }
     };
}

Error: class test.M.__anonclass10 has forward references
Error: no constructor for __anonclass10

Is this a compiler bug? There's no reason why the above shouldn't compile,  
no?


But that's not all, when I mark my class immutable...

immutable class M {
     this(byte a) { _a = a; }
     byte _a;
     byte a() { return _a; };
}

void main() {
     auto m = new class(4) M {
         this(byte a) { super(a); }
         override byte a() { return 3+_a; }
     };
}

... the compiler says: cannot implicitly convert expression (this) of type  
immutable(M) to test.M.
Same story for const classes. Again, compiler bug?


Tomek


More information about the Digitalmars-d-learn mailing list