weird behavior returning delegate

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon Jul 3 10:06:03 PDT 2006


Bruno Medeiros wrote:
> Chris Nicholson-Sauls wrote:
> 
>>
>> If in delegate literals we could declare static variables initialized 
>> from the local frame, then it could even have simply been this:
>>
>> # T delegate (T) foo (T) (T n) {
>> #   return (T i) {
>> #     static T x  = n ;
>> #     return   x += i ;
>> #   };
>> # }
>>
>> Alas.
>>
>> -- Chris Nicholson-Sauls
> 
> 
> Nope, even if the static variable were to be initialized at delegate 
> literal evaluation time, instead of delegate run time, it wouldn't work 
> as there is only one delegate "body instance", so each accumulator would 
> share the same static var.
> 

True enough.  If 'n' were part of the template's parameters then it would be a bit closer, 
as then there would be a seperate function for each starting value... but then you 
couldn't use 'foo(N)' more than once for any given N.  I guess the only way to pull this 
off is going to be with a class, like the other three examples.  Seems like a shame, though.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list