What's a condition system and why do you want one?(axisofeval.blogspot.com) |
What's a condition system and why do you want one?(axisofeval.blogspot.com) |
If the condition is not handled, signal returns nil.
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec...Like other interrupt mechanisms, it also gets used for error exits in languages that don't have them inherently, but it's a pretty bad way to do that.
He also points out how the signal(7) and Lisp conditions came out of Multics. If you consider it that way, it becomes clear that the Unix decision to overload signal(7) with two different functions is why it is such a poor interface and major source of pain.
The relative costs are dependent on the language implementation. longjmp() in C is only roughly as expensive as a normal function return, but of course setjmp() costs an extra two or three function calls' worth. And in many interpreters, exceptions are basically free.
(Except on MacOS, where setjmp() and longjmp() are an order of magnitude slower because they involve system calls.)
I had a discussion with a mainframer a couple of years back about what kinds of things people did in assembly that aren't reflected in C. One interesting technique he mentioned was incrementing the return address in case of error returns, so the function actually had two different possible points to return to; the calling code would look something like this (x86 version):
CALL SOMEROUTINE
JMP 1f
JMP ERROR
1 ; continue with normal processing
In case of error, the routine bumped its return address by the size of a jump instruction before returning to the JMP ERROR instruction.Such a pattern would be awfully disconcerting if you weren't used to it, of course.
Multiple return values were, of course, another thing he mentioned. Any caller-saved register can double as an additional return value.
You've also got to be careful about how your handler stack interacts with threads and/or coroutines.
For an example that I think same up pretty well, but certainly not as elegant as the system built into lisp, check out my "withrestart" python module: https://github.com/rfk/withrestart
https://github.com/gonzojive/paren-psos/commit/6578ad223515d...