// $HeadURL: svn://svn.berlios.de/dstress/trunk/run/t/this_13_C.d $
// $Date: 2007-01-01 03:59:08 +0800 (Mon, 01 Jan 2007) $
// $Author: thomask $
// @author@ Frank Benoit <[email protected]>
// @date@ 2006-10-09
// @uri@ http://d.puremagic.com/issues/show_bug.cgi?id=419
// @desc@ [Issue 419] New: Anonymous classes are not working.
module dstress.run.t.this_13_C;
interface I {
void get( char[] s );
}
class C{
void init(){
I i = new class(){
void get( char[] s ){
func();
}
};
}
void func( ){ }
}
int main(){
C c = new C();
c.init();
return 0;
}
compile result with:
bug.d(18): Error: cannot implicitly convert expression ((class __anonclass1 : Object
{
void get(char[] s)
{
assert(this,"null this"); // this line is quite unreadable
this.this.func(); // also this.this.func is dummy
}
C this;
}
) , new __anonclass1) of type dstress.run.t.this_13_C.C.init.__anonclass1 to dstress.run.t.this_13_C.I
Comment #1 by clugdbug — 2012-09-27T01:27:29Z
Original title:
bizzare toCBuffer result when emit error message for bad source code
Applies to both D1 and D2.
Reduced test case:
void f() {
int k = new class(){};
}
-----
bug.d(3): Error: cannot implicitly convert expression (class __anonclass1 : Object
{
void* this;
}
, new __anonclass1) of type bug.f.__anonclass1 to int
-----
Another example, not involving an error:
void f() {
pragma(msg, (new class(){ void get(char[] s){} }).stringof);
}
which prints invalid D code:
----------------------------------
class __anonclass1 : Object
{
void get(char[] s)
{
assert(this,"null this");
}
void* this;
}
, new __anonclass1
----------------------------------
When printing the expression I don't think it should be printing the class contents. Would be enough to display:
bug.d(3): Error: cannot implicitly convert expression `new __anonclass1` of type bug.f.__anonclass1 to int
Comment #2 by robert.schadek — 2024-12-13T17:48:35Z