Bug 15370 – Some way to manually allocate the closure for delegates to nested functions.

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-11-21T08:47:35Z
Last change time
2022-01-09T21:42:51Z
Assigned to
No Owner
Creator
joeyemmons

Comments

Comment #0 by joeyemmons — 2015-11-21T08:47:35Z
There has been a strong push to move away from the GC, being able to allocate closures with out it would be another step in that direction. As far as I can tell there is no current way to manually allocate a closure. It would be great if there was a solution that could work with allocators to be able to manually allocate closures. Example: auto foo(int x) { int bar(){ return x; } return &bar; // implicitly allocates a closure with the gc // No way to manually allocate it currently } Maybe it could be something as simple as __traits(setClosureAllocator, Mallocator.instance); to set the allocator for the closure of the current function. Allocators then would need to handle the case of freeing the delegate closure as well.
Comment #1 by pro.mathias.lang — 2022-01-09T21:42:51Z
You can manually allocate a closure by using a struct. You allocate the struct, and return it, its `opCall`, or any other member function. As for using allocators - I think we're still far from that. Anything of that magnitude would need to come in the form of a DIP - there's just too much implication to fully capture it here. Since it's possible to manually allocate, closing this as INVALID.