lambda function with "capture by value"

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 6 10:05:32 PDT 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 at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170806/70d56e18/attachment.sig>


More information about the Digitalmars-d-learn mailing list