Re: How to programmatically exit?



On Nov 1, 5:41 pm, Russell Wallace <russell.wall...@xxxxxxxxx> wrote:
On Nov 1, 12:08 pm, "Thomas F. Bur***" <tburd...@xxxxxxxxx> wrote:

Just a word of caution: do you really want to have NIL mean end-of-the-
world? If you ever end out throwing to nil on accident, you'll end out
not in the debugger but quietly exiting. It's probably not a big deal
(although the gun is pointed at your foot, the safety is on and the
chamber's empty) but I always call catch tags like yours above
something more like :goodbye-world or %exit or something.

What you're saying makes sense; I started off using nil to mean
basically "error parsing data file, so exit"; but then I added support
for processing several files in a batch, so I actually changed the
location of the catch, to mean "okay, ignore that file, but proceed to
the next one"; so I'll probably keep using nil to mean "done here, let
top-level decide what to do next".

This of course is an advantage of throw over explicit exit -- you can
change your mind about how to handle the situation, without having to
modify the low-level code.

Okay, that makes a lot of sense.

One last stylistic nit to pick: do you know about restarts, and
specifically ABORT? You've notice in your debugger that you often have
the option of invoking a restart called ABORT? There's a standard
function cl:abort that invokes it; there's also CONTINUE as a standard
name for a restart. If you have some concept of "the current
operation" in your application, it can be convenient to have ABORT,
well, abort it and return to the top-level loop, whatever that loop
may be. In Ltk, for example, we create an abort restart that aborts
handling the latest event (user input, button press, timeout, etc) and
returns to the main event loop. It interacts nicely with standard
debuggers.

The idiom is like this:

(loop while (keep-going-p)
do (restart-case
(do-one-iteration)
(abort ()
:report "Abort processing this file"
nil)))

Sometimes catch is better, sometimes restarts are more convenient,
just food for thought.

(OMG is that a pattern?! No, wait, it's an idiom. I guess I formatted
it differently?)
.