Re: Delegate, scope and associative array

2014-06-03 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Tuesday, 3 June 2014 at 05:40:44 UTC, Edwin van Leeuwen wrote: On Monday, 2 June 2014 at 23:44:01 UTC, Rene Zwanenburg wrote: On Monday, 2 June 2014 at 20:09:12 UTC, Edwin van Leeuwen wrote: As you may have guessed, a workaround is to copy the iteration variable yourself: unittest {

Re: Delegate, scope and associative array

2014-06-03 Thread Ali Çehreli via Digitalmars-d-learn
On 06/02/2014 10:40 PM, Edwin van Leeuwen wrote: On Monday, 2 June 2014 at 23:44:01 UTC, Rene Zwanenburg wrote: On Monday, 2 June 2014 at 20:09:12 UTC, Edwin van Leeuwen wrote: As you may have guessed, a workaround is to copy the iteration variable yourself: unittest { size_t

Re: Delegate, scope and associative array

2014-06-03 Thread Vlad Levenfeld via Digitalmars-d-learn
I've run across this myself. The workaround I used was to call a function from inside the foreach loop, and in that function you construct the delegate (with the index variable passed as a parameter).

Re: Delegate, scope and associative array

2014-06-03 Thread Vlad Levenfeld via Digitalmars-d-learn
Hah, sorry, I didn't read the last post. I did exactly what Ali just suggested.

Re: Delegate, scope and associative array

2014-06-03 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Tuesday, 3 June 2014 at 07:00:35 UTC, Ali Çehreli wrote: 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); } assert( events[1]()

Re: Delegate, scope and associative array

2014-06-03 Thread bearophile via Digitalmars-d-learn
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:

Re: Delegate, scope and associative array

2014-06-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On Mon, 02 Jun 2014 19:43:58 -0400, Rene Zwanenburg renezwanenb...@gmail.com wrote: On Monday, 2 June 2014 at 20:09:12 UTC, Edwin van Leeuwen wrote: I'm probably missing something basic, but I am confused by what is going on in the following code. unittest { size_t delegate()[size_t]

Re: Delegate, scope and associative array

2014-06-03 Thread via Digitalmars-d-learn
On Tuesday, 3 June 2014 at 13:30:13 UTC, Steven Schveighoffer wrote: There is a school of thought (to which I subscribe) that says you shouldn't allocate a separate closure for each loop iteration. Basically, a closure will only use the stack frame of the calling function, not any loop

Re: Delegate, scope and associative array

2014-06-02 Thread Rene Zwanenburg via Digitalmars-d-learn
On Monday, 2 June 2014 at 20:09:12 UTC, Edwin van Leeuwen wrote: I'm probably missing something basic, but I am confused by what is going on in the following code. unittest { size_t delegate()[size_t] events; foreach( i; 1..4 ) { events[i] = { return i; }; } writeln(

Re: Delegate, scope and associative array

2014-06-02 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Monday, 2 June 2014 at 23:44:01 UTC, Rene Zwanenburg wrote: On Monday, 2 June 2014 at 20:09:12 UTC, Edwin van Leeuwen wrote: As you may have guessed, a workaround is to copy the iteration variable yourself: unittest { size_t delegate()[size_t] events; foreach(_i; 1..4 ) {