Delegate, scope and associative array

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 3 01:06:04 PDT 2014


Ali Çehreli:

> Here is a workaround:
>
> unittest {
>     size_t delegate()[size_t] events;
>
>     auto makeClosure(size_t i) {
>         return { return i; };
>     }
>
>     foreach( i; 1..4 ) {
>         events[i] = makeClosure(i);
>     }

You can also use two lambdas to do that, without the 
"makeClosure":
http://rosettacode.org/wiki/Closures/Value_capture#Less_Functional_Version

Also don't forget do make the foreach argument immutable (I think 
this is a lost war for me. Even smart programmers like you stick 
to the defaults, even when they are a design mistake of the 
language. So immutability by default is very important).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list