Thread overview
Anonymous nested class of problems
Dec 12, 2009
Tomek Sowiński
Dec 12, 2009
Tomek Sowiński
Dec 12, 2009
div0
Dec 12, 2009
Tomek Sowiński
Dec 12, 2009
div0
Dec 12, 2009
Tomek Sowiński
Dec 12, 2009
Tomek Sowiński
December 12, 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
December 12, 2009
Dnia 12-12-2009 o 13:09:49 Tomek Sowiński <just@ask.me> napisał(a):

> Error: no constructor for __anonclass10

This one seems to be unrelated to anonymous stuff.

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

    static m = new M(3);
}

I get: Error: no constructor for M.


Tomek
December 12, 2009
Tomek Sowiñski wrote:
> Dnia 12-12-2009 o 13:09:49 Tomek Sowiñski <just@ask.me> napisa³(a):
> 
>> Error: no constructor for __anonclass10
> 
> This one seems to be unrelated to anonymous stuff.
> 
> class M {
>     this(byte a) { _a = a; }
>     byte _a;
>     byte a() { return _a; };
> 
>     static m = new M(3);
> }
> 
> I get: Error: no constructor for M.
> 
> 
> Tomek

Well for this one, you're doing it wrong. You want:

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

    static M m;

    static this() {
        m = new M(3);
    }
}

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
December 12, 2009
Dnia 12-12-2009 o 15:00:56 div0 <div0@users.sourceforge.net> napisał(a):

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Tomek Sowiñski wrote:
>> Dnia 12-12-2009 o 13:09:49 Tomek Sowiñski <just@ask.me> napisa³(a):
>>
>>> Error: no constructor for __anonclass10
>>
>> This one seems to be unrelated to anonymous stuff.
>>
>> class M {
>>     this(byte a) { _a = a; }
>>     byte _a;
>>     byte a() { return _a; };
>>
>>     static m = new M(3);
>> }
>>
>> I get: Error: no constructor for M.
>>
>>
>> Tomek
>
> Well for this one, you're doing it wrong. You want:
>
> class M {
>     this(byte a) { _a = a; }
>     byte _a;
>     byte a() { return _a; };
>
>     static M m;
>
>     static this() {
>         m = new M(3);
>     }
> }

So it's the same when I get the "non-constant expression..." error and have to put in static ctors? Then what's with the "no constructor for M"?


Tomek
December 12, 2009
Tomek Sowiński wrote:
> Dnia 12-12-2009 o 15:00:56 div0 <div0@users.sourceforge.net> napisał(a):
> 
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Tomek Sowiñski wrote:
>>> Dnia 12-12-2009 o 13:09:49 Tomek Sowiñski <just@ask.me> napisa³(a):
>>>
>>>> Error: no constructor for __anonclass10
>>>
>>> This one seems to be unrelated to anonymous stuff.
>>>
>>> class M {
>>>     this(byte a) { _a = a; }
>>>     byte _a;
>>>     byte a() { return _a; };
>>>
>>>     static m = new M(3);
>>> }
>>>
>>> I get: Error: no constructor for M.
>>>
>>>
>>> Tomek
>>
>> Well for this one, you're doing it wrong. You want:
>>
>> class M {
>>     this(byte a) { _a = a; }
>>     byte _a;
>>     byte a() { return _a; };
>>
>>     static M m;
>>
>>     static this() {
>>         m = new M(3);
>>     }
>> }
> 
> So it's the same when I get the "non-constant expression..." error and have to put in static ctors?

Yes I think.

> Then what's with the "no constructor for M"?

Don't know.

It's best not to read too much into DMDs error messages, they leave a lot to be desired.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
December 12, 2009
Dnia 12-12-2009 o 13:09:49 Tomek Sowiński <just@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
December 12, 2009
Dnia 13-12-2009 o 00:44:56 Tomek Sowiński <just@ask.me> napisał(a):

> Dnia 12-12-2009 o 13:09:49 Tomek Sowiński <just@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...

Should've DAFS first...
http://d.puremagic.com/issues/show_bug.cgi?id=2610
What's disturbing is that this bug is nearly a year old... Could this be that immutable classes lay there virtually unusable for so long?


Tomek