Thread overview
variable scope
Apr 11, 2005
Ben Hinkle
Apr 11, 2005
Manfred Nowak
Apr 11, 2005
Ben Hinkle
Apr 11, 2005
Sean Kelly
Apr 11, 2005
Sean Kelly
April 11, 2005
Is this a bug?

 int delegate () test(int num) {

  return delegate int() {
   return num;
  };
 }

 writefln("num: ", test(0)());


-- 
Miguel Ferreira Simões


April 11, 2005
"Miguel Ferreira Simões" <Kobold@netcabo.pt> wrote in message news:d3dopj$28a2$1@digitaldaemon.com...
> Is this a bug?
>
> int delegate () test(int num) {
>
>  return delegate int() {
>   return num;
>  };
> }
>
> writefln("num: ", test(0)());

no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).


April 11, 2005
> no - the stack of test(0), which contains num, is gone by the time the
> delegate is called. You need to store num on the heap if you need it to
> live beyond the call to test(0).

You are right! Thanks!


April 11, 2005
"Ben Hinkle" <ben.hinkle@gmail.com> wrote:

[...]
> the stack of test(0), which contains num, is gone by the
> time the delegate is called.

According to the specs this behaviour _is_ buggy, because the specs only make accesses to local _variables_ illegal, not to parameters.

But how about this:

<code>
import std.stdio;
void main(){
  int x=0;
  int delegate () y;
  int delegate () test(inout int num) {
    y= delegate int() {
      return num;
    };
    return y;
  }
  writefln("num: ", test(x)());
}
</code>

Still wrong result.

-manfred




April 11, 2005
"Manfred Nowak" <svv1999@hotmail.com> wrote in message news:d3e3vo$2mqn$1@digitaldaemon.com...
> "Ben Hinkle" <ben.hinkle@gmail.com> wrote:
>
> [...]
>> the stack of test(0), which contains num, is gone by the
>> time the delegate is called.
>
> According to the specs this behaviour _is_ buggy, because the specs only make accesses to local _variables_ illegal, not to parameters.

I'm not sure what section you are looking at but if you have a suggestion for clarifying the doc I'd add a note to the wiki for documentation feedback. About the specific issue I doubt Walter intends for D to have different lifetimes for local variables and parameters.

> But how about this:
>
> <code>
> import std.stdio;
> void main(){
>  int x=0;
>  int delegate () y;
>  int delegate () test(inout int num) {
>    y= delegate int() {
>      return num;
>    };
>    return y;
>  }
>  writefln("num: ", test(x)());
> }
> </code>
>
> Still wrong result.

Correct. The delegate is still accessing the parameter "num". It doesn't matter that num is a pointer or reference to another location. To work properly the delegate can't refer to anything that is not on the stack when the delegate is called.


April 11, 2005
In article <d3dpvd$29n6$1@digitaldaemon.com>, Ben Hinkle says...
>
>
>"Miguel Ferreira Simões" <Kobold@netcabo.pt> wrote in message news:d3dopj$28a2$1@digitaldaemon.com...
>> Is this a bug?
>>
>> int delegate () test(int num) {
>>
>>  return delegate int() {
>>   return num;
>>  };
>> }
>>
>> writefln("num: ", test(0)());
>
>no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).

Really?  But the delegate is called before the function exits.  I would expect the stack frame to be intact at this point.


Sean


April 11, 2005
In article <d3ehk4$61r$1@digitaldaemon.com>, Sean Kelly says...
>
>In article <d3dpvd$29n6$1@digitaldaemon.com>, Ben Hinkle says...
>>
>>
>>"Miguel Ferreira Simões" <Kobold@netcabo.pt> wrote in message news:d3dopj$28a2$1@digitaldaemon.com...
>>> Is this a bug?
>>>
>>> int delegate () test(int num) {
>>>
>>>  return delegate int() {
>>>   return num;
>>>  };
>>> }
>>>
>>> writefln("num: ", test(0)());
>>
>>no - the stack of test(0), which contains num, is gone by the time the delegate is called. You need to store num on the heap if you need it to live beyond the call to test(0).
>
>Really?  But the delegate is called before the function exits.  I would expect the stack frame to be intact at this point.

I take it back.  I misread the return value for test.  I thought it returned an int, not a delegate.


Sean