On Wed, May 04, 2022 at 10:04:53PM +0000, forkit via Digitalmars-d-learn wrote: > On Wednesday, 4 May 2022 at 21:55:18 UTC, H. S. Teoh wrote: > > On Wed, May 04, 2022 at 09:46:50PM +0000, forkit via Digitalmars-d-learn > > wrote: [...] [...] > > > To deny a programmer the option to release the memory that was GC > > > allocated within a particular scope, to be release immediately > > > after that scope exits, seems kinda cruel. > > [...] > > > > scope ptr = GC.malloc(size); > > scope(exit) GC.free(ptr); > > > > ... // use ptr however you like until end of scope [...] > that's cruel! > > I just want 'scope-based deallocation of GC allocated memory'. > > I just want to write one word for this to happen -> 'inscope'
------- import std; // Put this in some common module auto scoped(T, Args...)(Args args) { static struct Result { private T* payload; ref T get() { return *payload; } alias get this; ~this() { import core.memory : GC; writeln("dtor"); GC.free(payload); } } return Result(new T(args)); } // Then you can use it in just a single line, as below void main() { struct MyType { int blah, blahblah; } auto data = scoped!MyType(10, 20); // <--- like this data.blah = 123; // use data as you like // automatically frees on scope exit } ------- T -- Тише едешь, дальше будешь.