Bug 24046 – static destructors should be allowed in function bodies

Status
NEW
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-07-13T17:00:33Z
Last change time
2024-12-13T19:30:10Z
Assigned to
No Owner
Creator
basile-z
Moved to GitHub: dmd#20312 →

Comments

Comment #0 by b2.temp — 2023-07-13T17:00:33Z
Here is why : as we can hide static variables in function bodies there should be a way to cleanup them. A way to do that would be to allow local static destructors, for example ```d struct S { } S getS() { static S result; static ~this() // run when the program finishes { destroy(result); } return result ? result : (result = new S); } ```
Comment #1 by maxsamukha — 2023-07-13T17:36:28Z
While I agree that function-local static constructors/destructors should just work, you can work around this bug by wrapping the destructor in a struct.
Comment #2 by razvan.nitu1305 — 2023-07-14T09:55:15Z
Maybe an alternative would be for the compiler to automatically call the destructor of all static variables once the program ends.
Comment #3 by robert.schadek — 2024-12-13T19:30:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20312 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB