Thread overview | |||||
---|---|---|---|---|---|
|
August 23, 2002 Alias and Typedef treated differently for delegates | ||||
---|---|---|---|---|
| ||||
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; } |
August 23, 2002 Re: Alias and Typedef treated differently for delegates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joe Battelle | On Fri, 23 Aug 2002 02:19:13 +0000 (UTC) Joe Battelle <Joe_member@pathlink.com> wrote:
> 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.
Of course. Typedef creates a new type, based on the one you specify. It will
be implicitly
convertable to that type, but you must use a cast to convert base type to
typedef:
typedef int number;
int n;
number m;
n = m; // okay, number is "derived" from int
m = n; // error, should use cast()
Note, the compiler says "cannot implicitly convert int to number", but don't
let the buggy
erro message fool you - it actually complains about "m = n" (BTW seems like a
bug!).
This applies to all types, delegates included.
|
August 23, 2002 Re: Alias and Typedef treated differently for delegates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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. |
Copyright © 1999-2021 by the D Language Foundation