August 06, 2017
On Saturday, 5 August 2017 at 19:58:08 UTC, Temtaime wrote:
>  		(k){ dgs[k] = {writefln("%s", k); }; }(i);

Yeah, that's how I'd do it - make a function taking arguments by value that return the delegate you actually want to store. (Also use this pattern in Javascript btw for its `var`, though JS now has `let` which works without this trick... and D is supposed to work like JS `let` it is just buggy).

You could also define a struct with members for the values you want, populate it, and pass one of its methods as your delegate. It is syntactically the heaviest but does give the most precise control (and you can pass the struct itself by value to avoid the memory allocation entirely if you want).

But for the loop, the pattern Temtaime wrote is how I'd prolly do it.
August 06, 2017
On Sun, 2017-08-06 at 12:50 +0000, Adam D. Ruppe via Digitalmars-d-learn wrote:
> On Saturday, 5 August 2017 at 19:58:08 UTC, Temtaime wrote:
> >  		(k){ dgs[k] = {writefln("%s", k); }; }(i);
> 
> Yeah, that's how I'd do it - make a function taking arguments by value that return the delegate you actually want to store. (Also use this pattern in Javascript btw for its `var`, though JS now has `let` which works without this trick... and D is supposed to work like JS `let` it is just buggy).
> 

Assuming I have understood the problem, this is the solution necessary in Python: you have to use an enclosing function around the function so as to capture the value using a parameter. I think Java requires the same.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

August 06, 2017
On Sunday, 6 August 2017 at 12:50:22 UTC, Adam D. Ruppe wrote:
> On Saturday, 5 August 2017 at 19:58:08 UTC, Temtaime wrote:
>>  		(k){ dgs[k] = {writefln("%s", k); }; }(i);
>
> Yeah, that's how I'd do it - make a function taking arguments by value that return the delegate you actually want to store. (Also use this pattern in Javascript btw for its `var`, though JS now has `let` which works without this trick... and D is supposed to work like JS `let` it is just buggy).
>
> You could also define a struct with members for the values you want, populate it, and pass one of its methods as your delegate. It is syntactically the heaviest but does give the most precise control (and you can pass the struct itself by value to avoid the memory allocation entirely if you want).
>
> But for the loop, the pattern Temtaime wrote is how I'd prolly do it.

I like the (kinda cryptic IMO) look of this '(k){...}(i)' construction. But for my actual code I went with struct+opCall without any delegate at all.

Anyway, thanks for all your suggestions.
August 06, 2017
On Saturday, 5 August 2017 at 18:17:49 UTC, Simon Bürger wrote:
> If a lambda function uses a local variable, that variable is captured using a hidden this-pointer. But this capturing is always by reference. Example:
>
>     int i = 1;
>     auto dg = (){ writefln("%s", i); };
>     i = 2;
>     dg(); // prints '2'
>
> Is there a way to make the delegate "capture by value" so that the call prints '1'?
>
> Note that in C++, both variants are available using
>    [&]() { printf("%d", i); }
> and
>    [=]() { printf("%d", i); }
> respectively.

I asked about this a couple of day ago:
http://forum.dlang.org/thread/ckkswkkvhfojbcczijim@forum.dlang.org

The problem is that the lambda captures the entire enclosing stack frame. This is actually a bug because the lambda should only capture the enclosing *scope*, not the entire stack frame of the function. So even if you were to copy `i` into a temporary in some nested scope where a lambda was declared (this works in C# for example), that temporary would still reside in the same stack frame as the outer `i`, which means there would still be only one copy of it.

There is a workaround in Timon's post here:
http://forum.dlang.org/post/om2aqp$2e9t$1@digitalmars.com

Basically, that workaround wraps the nested scope in another lambda to force the creation of a separate stack frame.



1 2
Next ›   Last »