17 ms·
“ Nested functions can even goto back into their parent function, allowing for nonlocal exits to break out of nested functions like Smalltalk blocks, allowing c
by actionfromafar 3y ago
“ Nested functions can even goto back into their parent function, allowing for nonlocal exits to break out of nested functions like Smalltalk blocks, allowing control flow-like functions to be built using them.”
Wild!
Makes one wonder how something could have evolved if it took C and made it more “functional” and/or smalltalk-ish, without going the dynamic way of ObjectiveC.
I don’t exactly love C++ but I find it very useful. Maybe I could have loved that other never conceived C offspring.
- derefr 3y agoNote that this functionality is already in C — in the form of the setjmp/longjmp functions, which indeed allow you to have a parent function that contains "global" error-handling, and then a hierarchy of child functions designed to only be executed from that parent, that can "jump out" to the parent's error handler. (But where "parent" and "child" here are just a conceptual relationship the programmer is keeping track of, not anything the compiler knows about.) The difference is that High C gives you lexically-nested function definitions, thus making labels into things like local variables that have scope + shadowing. And so it becomes sensible — necessary, really, to have a coherent semantics — to extend the (runtime, dynamic) longjmp, into a compile-time-targeted, lexical, syntax-sugared version that jumps to in-scope labels (incl. ones defined outside the current lexically-nested function definition.) If you didn't, then you'd have to decide on what a `goto` to an in-scope but out-of-function label would do instead — compiler error, maybe? — and whatever you choose, it would probably be just as hard to implement as just making it work. (Most of the effort is in extending the CFG to allow some analysis pass to notice that this is happening, after all.)