4 ms·
Beyond Exception Handling: Conditions and Restarts
- awj 15y agoThis is the one feature I wish I could steal from Common Lisp in almost every language I work with. It would be much easier to put in than almost any other big lisp feature, and damn would it make a lot of things easier.
- sedachv 15y agoThis is something you can do in most languages if all the code that needs restarts follows a convention/protocol. Red Daly implemented CL condition handling for JavaScript for the Parenscript CL-to-JS compiler: https://github.com/gonzojive/paren-psos/commit/6578ad223515dc2c1ddf49346f4baf7c3bee37c4 https://github.com/gonzojive/paren-psos/commit/6578ad223515d... The compiled CL code knows about the convention and follows it, but other JavaScript code doesn't and isn't affected by it.
- awj 15y agoYeah, that's why I talk about stealing it at the language level. Although it's possible to use this based on conventions, the real power comes from being able to use it to cover weird conditions while gluing various libraries together. If your language already has a relatively similar tool (i.e. "traditional" exceptions) there's basically no chance that anything you pick up will support conditions. That said, it's a wonderful model to know about/follow if you come into the rare case that makes reimplementing it where you can useful.
- sedachv 15y agoThis is true for Common Lisp libraries as well - if they don't offer restarts and just throw exceptions, it looks like it does in any other language. Developing a good protocol for signalling and handling errors takes work.
- someone13 15y agoIs there any other language that implements this? I'll freely admit that I'm no expert on Lisp, so I've tried digging through the code, and I sorta get what the concept is, but I don't really understand it. Any other languages have the same idea?
- briantrice 15y agoDylan does, and my language Slate (http://www.slatelanguage.org http://www.slatelanguage.org) does, as well as its related younger siblings, Atomo (http://atomo-lang.org http://atomo-lang.org) and Atomy (http://atomy-lang.org http://atomy-lang.org). And all of these implement them in the context of an object model, rather than carefully avoiding CLOS dependencies as in CL.
- bakkdoor 15y agoI just added conditions & restarts today to Fancy (http://fancy-lang.org/ http://fancy-lang.org/). It's currently in a branch but it works =).
- briantrice 15y agoThanks for doing that. More implementations means there's a wider spectrum of places for developers to learn these tools and maybe even improve on them.
- gigamonkey 15y agoI believe Mesa did. On the other hand, Stroustrup claims (in the Design and Evolution of C++, I think) that the Cedar system written in Mesa basically never used the ability to restart as a reason why C++ doesn't have continuable exceptions. I've never been able to track down more information about that than his claim though.
- sedachv 15y agoKent Pitman wrote a pretty thorough history: http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html http://www.nhplace.com/kent/Papers/Condition-Handling-2001.h...
- IgorPartola 15y agoDoes this basically mean that CL provides a mechanism that's lower level than what is otherwise equivalent to exceptions/warnings, and that this in turn enables things like restarts? Is this analogous to the relationship between higher level control structures such as if/for/while/break/continue vs the lower-level goto? Lastly, can some of these features not be implemented with exceptions?: def parse_log_file(f): result = [] for line in f.readlines(): # Fails for large files parsed_line = None retries = 0 while retries < 5: try: result.append(parse_log_entry(line)) break except MalformedLogEntry: line = fix(line) retries += 1 return result
- Kototama 15y agoYour code is not equivalent. There is a separation of concerns that is possible with conditions/restarts that is not possible with exceptions. The calling code can choose the appropriate restarts base on information that the callee code does not - and should not - have access to.
- IgorPartola 15y ago> The calling code can choose the appropriate restarts base on information that the callee code does not - and should not - have access to. In the common case, is that not equivalent to the caller catching specific errors by wrapping the callee in a try/except? Could you elaborate?
- Kototama 15y agoBut this is assuming the caller has enough information to solve the problem, which is not always the case (the callee could have information on the structure of the disk that are not relevant to the caller for instance). Conditions/restarts make a separation of concerns, particularly when the caller knows which of the restart is the more appropriate, if the callee has not enough information/context to choose, and if the callee has more information on how to solve the problem. I guess the separation can be summarized by: difference between knowing WHAT and HOW to do something. A useful abstraction I guess.