www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Anonymous nested class of problems

reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
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
Dec 12 2009
next sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 12-12-2009 o 13:09:49 Tomek Sowi=F1ski <just ask.me> napisa=B3(a):

 Error: no constructor for __anonclass10
This one seems to be unrelated to anonymous stuff. class M { this(byte a) { _a =3D a; } byte _a; byte a() { return _a; }; static m =3D new M(3); } I get: Error: no constructor for M. Tomek
Dec 12 2009
parent reply div0 <div0 users.sourceforge.net> writes:
-----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); } } - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLI6IYT9LetA9XoXwRAmmrAKCQYcX8qyXXxYMau4gVyAbEM3PWpACfS0uU 1wc11d9lU608J+ZSiEH77AE= =svdA -----END PGP SIGNATURE-----
Dec 12 2009
parent reply =?utf-8?B?VG9tZWsgU293acWEc2tp?= <just ask.me> writes:
Dnia 12-12-2009 o 15:00:56 div0 <div0 users.sourceforge.net> napisa=C5=82=
(a):

 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 Tomek Sowi=C3=B1ski wrote:
 Dnia 12-12-2009 o 13:09:49 Tomek Sowi=C3=B1ski <just ask.me> napisa=C2=
=B3(a):
 Error: no constructor for __anonclass10
This one seems to be unrelated to anonymous stuff. class M { this(byte a) { _a =3D a; } byte _a; byte a() { return _a; }; static m =3D 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 =3D a; } byte _a; byte a() { return _a; }; static M m; static this() { m =3D 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
Dec 12 2009
parent div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLJB/3T9LetA9XoXwRAtSOAJ9tOP6uJiL6bvxtKZmiW8EMTDmOcQCfbOxs 6f+K154WRQu9NkYKdcDJ2fU= =ueMn -----END PGP SIGNATURE-----
Dec 12 2009
prev sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 12-12-2009 o 13:09:49 Tomek Sowi=F1ski <just ask.me> napisa=B3(a):

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

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

 void main() {
      auto m =3D 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 =3D 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 =3D 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(Pod= M) = to test.PodM Compiler bug I guess... Tomek
Dec 12 2009
parent =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 13-12-2009 o 00:44:56 Tomek Sowi=F1ski <just ask.me> napisa=B3(a):

 Dnia 12-12-2009 o 13:09:49 Tomek Sowi=F1ski <just ask.me> napisa=B3(a)=
:
 But that's not all, when I mark my class immutable...

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

 void main() {
      auto m =3D 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 =3D 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 =3D 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=3D2610 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
Dec 12 2009