Wrong implicit delegate conversion?
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Feb 23 16:18:12 PST 2007
Manfred Nowak wrote:
> Here a delegate from class C seems to be implicitely converted to a
> delegate of the module.
>
> import std.stdio;
> alias void delegate( int , inout int) GETTER;
> class C{
> int i=3;
> GETTER getter(){
> return delegate void(int i, inout int o){ o= 2*this.i;};
> }
> }
> void main(){
> auto c= new C;
> int i=0;
> c.getter()(0,i);
> writefln( i); //works: outputs 6
> GETTER q= c.getter(); // implicit conversion?
> q(0, i);
> writefln( i); //random result
> }
First off, on my machine (DMD/Linux):
---
urxae at urxae:~/tmp$ dmd -run test.d
269043800
125854208
---
The delegate isn't one 'of' the class. It's 'of' the C.getter() stack
frame, which means it's invalid after the getter() function returned.
*Never*[1] return a delegate literal from a function. Unpredictable
results will follow.
And what exactly do you mean by "a delegate of the module"? AFAIK there
is no such thing.
[1]: Well, at least not until Walter implements full closures that
capture local variables.
More information about the Digitalmars-d-bugs
mailing list