@nogc and lambdas

Sebastiaan Koppe mail at skoppe.eu
Wed Oct 30 16:44:09 UTC 2024


On Wednesday, 30 October 2024 at 15:25:06 UTC, Manu wrote:
> The problem here is that the language is trying to allocate a 
> closure
> around `this` because the lambda calls another method.
> This is totally redundant; there's no need to allocate a 
> closure if `this`
> is the only thing that's captured... because it should just 
> accept `this`
> as the context directly!
> This lambda isn't a closure, it's a METHOD.
> The compiler needs to determine that `this` is the only 
> capture, and then
> abandon the closure and synthesise the lambda as a regular 
> method.

I am starting to dislike closures altogether, and would prefer a 
way to explicit handle captures. For your use case, perhaps 
something like:

```
acceptsCallback(()[this=&this]{ doSomething(); });
```


More information about the Digitalmars-d mailing list