We need to have a way to say "convert this nested function into a struct"

Meta via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 6 11:36:23 PDT 2015


On Saturday, 6 June 2015 at 18:32:14 UTC, Meta wrote:
> On Saturday, 6 June 2015 at 06:59:26 UTC, Jonathan M Davis 
> wrote:
>> That being said, we really should find a way to make it so 
>> that lambda's don't turn into delegates unless they really 
>> need to. In many, many cases, they should be plenty efficient 
>> without having to force the issue with functors, but they 
>> aren't, because we allocate for them unnecessarily. I don't 
>> know how easy it'll be though for the compiler devs to figure 
>> out how to optimize that, since sometimes you _do_ need to 
>> allocate a closure.
>
> int n = 2;
>
> auto r1 = [1, 2, 3].map!(x => x + n); //Ok
> auto r2 = [1, 2, 3].map!(function(x) => x + n); //Error
> auto r3 = [1, 2, 3].map!(curry!(function(x, n) => x + n, n)); 
> //Ok
>
> IMO this is pretty much the same thing as copying the variables 
> you want to close over into a struct, with the advantage that 
> we can do it today. The only thing is that you have to specify 
> which variables you want to copy, which isn't necessarily a bad 
> thing.

And apparently curry actually allocates a closure. Nevermind 
that, then.


More information about the Digitalmars-d mailing list