www.digitalmars.com         C & C++   DMDScript  

D - Alias and Typedef treated differently for delegates

reply Joe Battelle <Joe_member pathlink.com> writes:
Why will the following code compile when DG is an alias, but not when it is a
unique type?  Can delegates not be typedef'd?  The compiler bawks at the
assignment of the delegate, not the typedef--or even the member function
returning DG.

Example code:
-----------------------------
version(wontcompile) {
typedef void delegate() DG;
} else {
alias void delegate() DG;
}

class A {
void foo() { printf("hi joe"); }
DG pfoo() { return &this.foo; } //this is ok
}

int main(char argc[][])
{
A a = new A;
//these won't compile with a strict type
DG dg = &a.foo;
dg();
DG dg = a.pfoo();
dg();
return 0;
}
Aug 22 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Fri=2C 23 Aug 2002 02=3A19=3A13 +0000 =28UTC=29 Joe Battelle 
=3CJoe=5Fmember=40pathlink=2Ecom=3E wrote=3A

=3E Why will the following code compile when DG is an alias=2C but not when it
is a
=3E unique type=3F  Can delegates not be typedef'd=3F  The compiler bawks at the
=3E assignment of the delegate=2C not the typedef--or even the member function
=3E returning DG=2E

Of course=2E Typedef creates a new type=2C based on the one you specify=2E It
will 
be implicitly
convertable to that type=2C but you must use a cast to convert base type to 
typedef=3A

=09typedef int number=3B

=09int n=3B
=09number m=3B

=09n =3D m=3B=09=2F=2F okay=2C number is =22derived=22 from int
=09m =3D n=3B=09=2F=2F error=2C should use cast=28=29

Note=2C the compiler says =22cannot implicitly convert int to number=22=2C but
don't 
let the buggy
erro message fool you - it actually complains about =22m =3D n=22 =28BTW seems
like a 
bug!=29=2E
This applies to all types=2C delegates included=2E
Aug 22 2002
parent Joe Battelle <Joe_member pathlink.com> writes:
Yeah, I got that about typedefs.  Sorry, I reported the bug slightly wrong, the
message is: if.d(23): function expected before (), not 'DG'.  

So it gets past the assignment.  Indeed no cast seems to be needed on either the
return &this.foo or the assignment of dg=&a.foo.  Maybe the compiler is lax on
type checking these. Certainly no cast needs to be made for dg=a.pfoo() for the
return type is DG.

It looks like the compiler says what is has to the left of the function call
operator is not a function, and indeed it's not.  But it should be interpreted
as one.
Aug 22 2002