4 ms·
I figure closures would be hard to do in a programmer-friendly way without GC.
by bhiggins 16y ago
I figure closures would be hard to do in a programmer-friendly way without GC.
- albertzeyer 16y agosmart pointers would also do it.
- deleted 16y ago[deleted]
- mathias_10gen 16y agoC++0x managed to add closures without adding GC. The only issue is that if you want to the closure to outlive the current stack frame you have to capture by value ([=] rather than [&]) or use a shared_ptr (which is then copied by value).
- moultano 16y agoThe C++ closure library we use internally at Google has you specify whether the callback is permanent or not. If it isn't permanent, its deleted right after executing. It is a little painful maintaining the memory of your arguments, but people have generally converged to packing them all into a heap allocated struct and deleting when appropriate.
- davidxa 16y agoIf you implement closures as thunks, you can just free them when done, no need for GC. closure int foo(int x) {return x + y;} int(*foo_p)(int) = malloc(sizeof(foo)); memcpy(foo_p,&foo,sizeof(foo)); return foo_p; So, you can add working closures in one keyword (and possibly a special case for function pointer decay).