5 ms·
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 the
by howeyc 12y ago
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?
- pjmlp 12y agoNot really the same thing, but Eiffel does have retries. https://docs.eiffel.com/book/platform-specifics/exception-mechanism https://docs.eiffel.com/book/platform-specifics/exception-me...
- kbenson 12y agoPerl 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]. 1: http://perlcabal.org/syn/S04.html#Phasers http://perlcabal.org/syn/S04.html#Phasers 2: http://perlcabal.org/syn/S04.html#Control_Exceptions http://perlcabal.org/syn/S04.html#Control_Exceptions
- qnaal 12y agoperl6: super hyperdimensional prototype future programming language "It's not Lisp!"
- draegtun 12y agoHere is an interesting perl6 example which works in Rakudo: $ perl6 > $*PERL<name>; rakudo > $*PERL<compiler><ver>; 2014.03.01 > { CONTROL { print 1; $_.resume }; print 2; warn; say 3 } 213 ref: https://news.ycombinator.com/item?id=7192294 https://news.ycombinator.com/item?id=7192294 For a perl5 Condition System see this implementation - https://metacpan.org/pod/ConditionSystem https://metacpan.org/pod/ConditionSystem
- mst 12y agoThat one's still exception based. You wanted http://p3rl.org/Worlogog::Incident http://p3rl.org/Worlogog::Incident http://p3rl.org/Worlogog::Restart http://p3rl.org/Worlogog::Restart which uses Return::MultiLevel to do stack unwinds that can pass through try/catch constructs.
- DSMan195276 12y agoRust 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.
- steveklabnik 12y agoRust'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. 1: https://github.com/mozilla/rust/pull/12039 https://github.com/mozilla/rust/pull/12039
- stevelosh 12y agoFor conditions in Clojure check out Ribol: http://docs.caudate.me/ribol/ http://docs.caudate.me/ribol/
- kenko 12y agoalso here: https://github.com/bwo/conditions https://github.com/bwo/conditions though honestly Ribol is probably a better bet---just wanted to show that this kind of thing can be done as a library without too much complication.
- dragonwriter 12y ago> 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.)
- aufreak3 12y ago> 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] [1] https://code.google.com/p/muvee-symbolic-expressions/ https://code.google.com/p/muvee-symbolic-expressions/ [2] https://code.google.com/p/muvee-symbolic-expressions/wiki/ExceptionHandling https://code.google.com/p/muvee-symbolic-expressions/wiki/Ex... (Apologies for shameless plug) -- 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.
- BruceM 12y ago> Are there any other languages that allow the calling scope to specify how lower-level functions handle errors without unwinding the stack? Dylan (http://opendylan.org/ http://opendylan.org/) has them, but that's because some of the same people who standardized Common Lisp went on to design Dylan.
- aufreak3 12y ago> 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.