Value closures (no GC allocation)

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Mon May 29 15:05:58 PDT 2017


On Monday, 29 May 2017 at 21:26:11 UTC, MakersF wrote:
> 1) Capture by value. This is the ability to have a lambda which 
> behaves as expected in the example Vittorio showed

That's a known bug in the implementation; it is supposed to work 
that way now.

(Though the bug has been open for so long I treat it as intended 
and tell people how to work with it, it isn't actually what the 
design says.)

> 1) The lambda is not compatible with a delegate anymore
>
> 2) There need to be a special notation for reference capture

That's true, but that's the whole point of the new thing. The 
auto-generated struct btw can be converted to a delegate with 
`&obj.opCall` or copied into a manually allocated buffer with 
`memcpy(buffer, &obj, obj.sizeof)`, just like the manually 
written struct to do the same. (and might have convenience 
methods, e.g. toDelegate and copyInto, to do these things too).


> - if capture is provided, then the lambda is rewritten as a 
> struct, which the explicit variables in the capture list being 
> captured by value, and the remaining being captured by 
> reference (so there would still be implicit capture, which is 
> considered convenient in many programming languages).

Eh, if you do any explicit, I think it should be ALL explicit. 
There's value in the simple proposals to syntax rewrite into a 
struct... but going beyond that I think is a negative and should 
not be in this proposal.


More information about the Digitalmars-d mailing list