Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 26, 2013 Modifing local variables with anonymous delegate? | ||||
---|---|---|---|---|
| ||||
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 |
December 26, 2013 Re: Modifing local variables with anonymous delegate? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gordon | 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
---
|
December 26, 2013 Re: Modifing local variables with anonymous delegate? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gordon | 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.
|
December 27, 2013 Re: Modifing local variables with anonymous delegate? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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!
|
Copyright © 1999-2021 by the D Language Foundation