2 ms·
This is mostly a bad idea. (defun oblivion (thunk) (let ((successfulp nil)) (unwind-protect (multiple-value-prog1 (funcall thunk)
by kazinator 5y ago
This is mostly a bad idea.
(defun oblivion (thunk)
(let ((successfulp nil))
(unwind-protect (multiple-value-prog1 (funcall thunk)
(setf successfulp t))
(unless successfulp
(error "STOP RIGHT THERE CRIMINAL SCUM")))))
Supose (funcall thunk) signaled a very well thought-out error, filled with useful information. The error found a handler somewhere, and that handler found the perfect place to transfer control to.
But, oops, you hamfistedly intervened in that, and replaced that with your generic error.
You sometimes need let over unwind-protect not to throw an error, but to know whether or not to clean something up.
(defun do-it (handle)
(let ((transaction-completed nil))
(unwind-protect (progn (prepare-transaction handle)
(commit-transaction handle)
(setf transaction-completed t))
(unless transaction-completed
(roll-back handle)))))
The cleanup handlers of an unwind-protect always execute. But you don't always need them to do their clean-up. In this case, we want to abort the partially done transaction, but avoid doing so in the happy case.
I think this makes for a better illustration of why you're sometimes only interested in the non-local exit.