To fail gracefully, an operation needs to manipulate a different stack in order to be able to recover from an error (if an error occurs, the stack is in an undetermined state).
A try block can terminate in two ways:
- The computation succeeded. In this case, its stack contents are inserted in the main stack
- The computation fails. In this case, its stack is discarded and
a special quoted symbol is put on the main stack (let's say this
symbol is
:$try/failed
)
A try block would be called from Shirka this way:
[stack] [operation] try
This does not require any syntactic sugar. It could be used with prefix notation:
-- (prepare a list to use as a stack)
(try)
[ do-something ]
Implementing a rescue block then becomes easy:
(=> rescue)
[ => $rescue/op
>> :$try/failed =
[ << $rescue/op ] !? ]
Pairing try
and rescue
becomes surprisingly natural:
(try)
[ dangerous-stuff ]
(rescue)
[ cleaning ]
other-operations