www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Modifing local variables with anonymous delegate?

reply "Gordon" <me home.com> writes:
Hello,

A question regarding delegates and local variables:
I have the following code, which seems to indicate the delegate 
function can access "b", but makes a private copy of it, instead 
of using the "real" b.
---
import std.stdio;
import std.algorithm;

void main()
{
         int[] a = [1,1,1];
         int b = 42;

         auto c = map! ( delegate(x) { ++b; return x+b ; } )(a);

         writeln("a = ",a);
         writeln("b = ",b);
         writeln("c = ",c);
}
---

--- The output ---
$ rdmd test.d
a = [1, 1, 1]
b = 42
c = [44, 45, 46]
---

Because the values in "c" are increasing, it means there is some 
copy of "b" which is incremented (from inside the delegate 
function).
But the "b" in "main" retains its original value of 42.

What's going on?
and is there a way to "fix" it (i.e. have a delegate update a 
local variable) ?

I've found few old discussions, mentioning "copy of stack 
variables" [1,2] and different kinds of closures [3], but I 
wasn't able to figure it out (I'm new to D).

Thanks,
  -gordon

[1] Anonymous Delegates(from 2008): 
http://forum.dlang.org/post/g36kpk$15rb$1 digitalmars.com
[2] Local vars in delegates(from 2009): 
http://forum.dlang.org/thread/hgshuk$1620$1 digitalmars.com
[3] Static vs Dynamic closures? (from 2008): 
http://forum.dlang.org/thread/ghu3m3$1e6o$1 digitalmars.com
Dec 26 2013
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/27/2013 12:23 AM, Gordon wrote:
 Hello,

 A question regarding delegates and local variables:
 I have the following code, which seems to indicate the delegate function
 can access "b", but makes a private copy of it, instead of using the
 "real" b.
 ---
 ...
Map is a lazy range. --- import std.stdio; import std.algorithm; void main(){ int[] a = [1,1,1]; int b = 42; auto c = map! ( delegate(x) { ++b; return x+b ; } )(a); writeln("a = ",a); writeln("b = ",b); writeln("c = ",c); writeln("b = ",b); } --- --- a = [1, 1, 1] b = 42 c = [44, 45, 46] b = 45 ---
Dec 26 2013
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 26 December 2013 at 23:23:02 UTC, Gordon wrote:
 But the "b" in "main" retains its original value of 42.
Try printing the b in main again AFTER printing c. You should see the change. std.algorithm for the most part doesn't actually do any of its calculations until it has to. This allows it to save speed skipping work it doesn't need, and make it possible to map infinite series and stuff like that. So the line c = map....; just prepares the calculation, it doesn't actually run the function. The writeln(c) forces evaluation. You can also force evaluation immediately by calling .array on the thing, which calculates it and puts the result in a new array.
Dec 26 2013
parent "Gordon" <me home.com> writes:
On Thursday, 26 December 2013 at 23:52:09 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 26 December 2013 at 23:23:02 UTC, Gordon wrote:
 But the "b" in "main" retains its original value of 42.
Try printing the b in main again AFTER printing c. You should see the change. std.algorithm for the most part doesn't actually do any of its calculations until it has to. This allows it to save speed skipping work it doesn't need, and make it possible to map infinite series and stuff like that. So the line c = map....; just prepares the calculation, it doesn't actually run the function. The writeln(c) forces evaluation. You can also force evaluation immediately by calling .array on the thing, which calculates it and puts the result in a new array.
Thanks!
Dec 26 2013