This was something I really loved during my time in Common Lisp. This was a gem that I was hoping would make it into Clojure, but I think around version 1.3 they decided to abandon the idea and add a library to "make anything throwable" which totally missed the point in my opinion.
Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack?
> Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack?
While it isn't a mainstream language by any means, a scheme dialect called muSE [1] that I developed for writing logic for automatic music video construction supports such resumes and retries. [2]
--
edit: I just saw that the clojure library "ribol" has an api design similar to muSE's in principle. A couple of design comments though -
1. The "on blah blah" is too much syntax for this. It prevents direct sharing of handlers between similar situations. Function argument pattern matching ought to be enough.
2. The retry options mechanism is somewhat limited - the options seem to apply only at the raise point itself (disclaimer: I've only had a quick glance at ribol). It is more useful to have multiple retry options at different scopes so you can partially unwind, try something, if that doesn't work unwind some more (perhaps involving cleanup), try something else, etc.
I think muSE's design is better on both these fronts.
Rust has 'Conditions', which basically allow the current function to run a closure when an error happens which tells it what to do on the error, and then continue the function call. I don't know much Rust or Common Lisp though, so I'm not sure if they're the same thing. Rust's condition functionality definitely doesn't unwind the stack when calling back though, because in Rust unwinding the stack isn't ever allowed because of memory safety issues.
Rust's conditions were actually removed. [1] They don't need language support, though, so you could re-add them as a library. When they existed, they were very close to Common Lisp conditions.
Perl 6 allows for handling exception in a same-scope CATCH block, so you can try to recover. It can probably be mixed with their phasers[1] in interesting ways. Also of interest might be control exceptions[2].
> Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack?
Any language with continuations can have it added as a library. Of course, not a lot of popular, non-Lisp languages these days have continuations... (Ruby kinda does -- it was a language-level feature in 1.8 but has been sidelined since as non-MRI implementations generally don't support them.)
> Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack?
another possible answer - Erlang.
While the look of the condition/restart scheme may be very different, such a condition/restart protocol can be implemented as message passing with a known global "condition handler" process I think.
A "raise" function can be implemented as a message-send to the condition handler process followed by an immediate wait-for-response. The handler process may terminate the raising process, replace its value with something, etc. depending on the installed handlers.
Here[0] is another example of extreme abuse of the condition system in order to construct "dynamic data pipelines" in Lisp. Roughly, it provides the ability of sending data to different parts of the call stack, and then returning back to continue processing. It can do all of this without any explicit use of variables, or without passing state around.
I originally had a section about reader macros but I ended up cutting it since the article was running a little long.
Reader macros are great, but they work best when a token can unambiguously be recognized by the reader as using some "special syntax" (usually involves a '#' and then another dispatch character preceding the new syntax).
This is great when you're coming up with a brand-new syntax. But if you're trying to match something that already exists, it's not easy
For those wondering about reader macros in Common Lisp, I wrote a tutorial a couple of months back that allows the Lisp reader to understand JSON: https://gist.github.com/chaitanyagupta/9324402
This reminds me of using doesNotRecognizeToSelector:/methodForSelector: in Objective-C to do similar tricks. For example, you can wire up a color class to respond to any hexColor selector by catching the unimplemented ones:
NSColor * color = [Colors h00FF00];
You can then parse the selector string when the runtime doesn't find an appropriate method for it. As with all these tricks, it is of course incredibly slower than just doing GetHexColor(char * hexString), but where's the fun in that right.
Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack?