Anonymous nested class of problems

Tomek Sowiński just at ask.me
Sat Dec 12 15:44:56 PST 2009


Dnia 12-12-2009 o 13:09:49 Tomek Sowiński <just at ask.me> napisał(a):

> 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?

It's not only about anonymous classes. Take a look at this:

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

immutable class PodM : M {
     this(int a) { super(a); }
     override int a() { return 3+_a; }
}

void main() {
     auto m = new PodM(3);
}

The above still throws the same error:
Error: cannot implicitly convert expression (this) of type immutable(M) to  
test.M
Error: cannot implicitly convert expression (this) of type immutable(PodM)  
to test.PodM

Compiler bug I guess...


Tomek


More information about the Digitalmars-d-learn mailing list