Let's get the semantic around closure fixed.
Steven Schveighoffer
schveiguy at gmail.com
Wed May 19 13:02:59 UTC 2021
On 5/18/21 12:47 PM, deadalnix wrote:
> Long story short: https://issues.dlang.org/show_bug.cgi?id=21929
>
> Closure do not respect scope the way they should. Let's fix it.
Thinking about how this would have to be implemented:
1. If you want to access a variable in a scope from both the closure and
the function itself, the variable has to be allocated on the heap
2. We need one allocation PER loop. If we do this the way normal
closures are done (i.e. allocate before the scope is entered), this
would be insanely costly for a loop.
3. It *could* allocate on demand. Basically, reserve stack space for the
captured variables, have a pointer to that stack space. When a closure
is used, copy that stack space to a heap allocation, and switch the
pointer to that heap block (so the function itself also refers to the
same data). This might be a reasonable tradeoff. But it has some
limitations -- like if you ever take the address of one of these
variables, that would also have to generate the allocated closure.
Of course, with Walter's chosen fix, only allowing capture of non-scoped
variables, all of this is moot. I kind of feel like that's a much
simpler (even if less convenient) solution.
And also, of course, can we please fix the issue where destroyed structs
are accessible from a delegate?
-Steve
More information about the Digitalmars-d
mailing list