D - Delegates and issues with Foo.bar()/&Foo.bar
- Joe Battelle (48/48) Aug 23 2002 The code below shows some oddities with Delegates and the Foo.bar() inst...
- Walter (1/1) Aug 24 2002 Thanks for the reports, I'll check into them.
The code below shows some oddities with Delegates and the Foo.bar() instance method invocation syntax and &Foo.bar delegate syntax. 1) You can't use A.bar() inside of A.foo() as an alternative to this. It may be by design, but I think it's weird. 2) You can use D.bar() inside of A.foo() when A implements D (interface), only you get a link time error, with the linker looking for a non abstract D.bar. I don't think D.bar() make sense--either way it's a compiler error. 3) You can create a delegate inside of a method using &A.foo instead of &this.foo. The result is, of course, that the A override is lost when the delegate is used: it behaves just like the &this.foo delegate. I think the compiler should not allow this &A.foo syntax for delegates defined inside of methods--or even better, add class overrides to the delegate functionality. 4) You can use &D.foo inside of A.pfoo() when A implements D (interface), and pfoo returns a delegate to method foo on this. I think this is weird in light of (2) not being allowed. ------------------ import c.stdio; alias void delegate() DG; interface D { abstract void foo(); } class A : D { void foo() { printf("A"); } //you can use this or D(!) but not A here DG pfoo() { return &D.foo; } void doo() { version(wontcompile) { //di.d(11): 'this' is required, but A is not a base class of A A.foo(); } version(wontlink) { //Error 42: Symbol Undefined _Ddi_D_foo_FZv D.foo(); //this shouldn't get past the compiler but it does //compiler thinks D is a base class of A so it allows it, but //then references a non abstract method of D! } } } class B : A { void foo() { printf("B"); } DG pfoo() { return &A.foo; } } int main(char[][] argv) { A a = new A; a.doo(); B b = new B; b.pfoo()(); //B (the A override in pfoo is lost by the delegate) return 0; }
Aug 23 2002