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