Thread overview | |||||
---|---|---|---|---|---|
|
February 23, 2007 Wrong implicit delegate conversion? | ||||
---|---|---|---|---|
| ||||
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 } -manfred |
February 24, 2007 Re: Wrong implicit delegate conversion? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Nowak | 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@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.
|
February 24, 2007 Re: Wrong implicit delegate conversion? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel wrote > it's invalid after the getter() function returned. > And what exactly do you mean by "a delegate of the module"? AFAIK there is no such thing. Thank you, you are right with both remarks. I was simply unable to detect my own fault. -manfred |
Copyright © 1999-2021 by the D Language Foundation