5 ms·
You simply can't do upward funargs in a C-family language without breaking the memory and activation-record model of the language. For example, let's say C++ ha
by bitwize 15y ago
You simply can't do upward funargs in a C-family language without breaking the memory and activation-record model of the language. For example, let's say C++ had upward funargs. Any time you return a function value, or otherwise keep it around for longer than the lifetime of the function activation in which it is activated, any free local variables in the closure which are captured by the containing environment must refer to locations on the heap. (Copying them by-value breaks the semantics of closures.) This conflicts with the assumption in C++ that auto variables local to a function activation are part of the function's activation record on the stack.
You could get around this problem with spaghetti stacks or something, but you'd need to find a way to free the activation records that are floating around after their enclosing scopes have expired -- enter garbage collection which you do NOT want to require for C++.
That's the problem with Lisp, it's almost all-or-nothing. If you want to correctly include some of the benefits of the Lisp execution model -- like lambdas -- you need to accept the whole thing, lock, stock, and barrel. Including heap-allocated local vars and the garbage collector. (And yes -- Python, Ruby, Haskell, and Standard ML have a "Lisp execution model" in this sense.)
So we get compromises and hulking abominations like C++0x lambdas or -- worse yet -- their predecessor, Boost Lambdas.
tl;dr: Upward funargs are hard, let's go shopping.
- ori_b 15y agoYou can do manual closure management. int f() { int x = 42; return dupclosure(^(){return x+10}}; } int g() { int (^fn)(); fn = f(); fn(); freeclosure(fn); } It's not quite as pretty, but it's workable, and in line with a C-family language's semantics.
- seabee 15y agoHowever it does require severe adjustments to the lifetime rules for automatic variables, and you have to be mindful of e.g. custom allocators.
- ori_b 15y agoCustom allocators are relatively easy handle with an API something like: void* closureheap(void (^)()) size_t closuresize(void(^)()) Or, well, anything else that allows you to separate the step of allocating memory from it's initialization. (There are probably more representation-independent APIs that would be better, but this was just off the top of my head) And, yes, mutable captured variables will not be shared across multiple duplications. Somewhat unconventional, but given the mental model of copying closures, I don't think it's a surprising behavior.
- barrkel 15y agoI'm aware of all the issues; I implemented the feature in a commercial language that's semantically very similar to C (Delphi, a variant of Pascal). My point is that there are compromises, like reference counting, that make the whole thing much easier to use. Yes, reference counting is a form of GC, but it's also deterministic and localized, and with careful selection of implementation primitives in the runtime library, potentially open to user fiddling too (as C++ users are wont to do).
- comex 15y ago> [...] any free local variables in the closure which are captured by the containing environment must refer to locations on the heap. Objective-C blocks automatically copy those variables to the heap. (Which is not to say they're not a bunch of compromises.)