Modifing local variables with anonymous delegate?

Timon Gehr timon.gehr at gmx.ch
Thu Dec 26 15:49:46 PST 2013


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
---


More information about the Digitalmars-d mailing list