Controlling loop flow with simple restarts
Sometimes you know in advance how you want to change control flow in a loop. Other times you might want to defer that decision by offering a restart. For example, here’s a loop with a pair of with-simple-restarts:
(with-simple-restart (stop-processing "Stop processing users")
(dolist (user (pending-user-list))
(with-simple-restart (skip "Skip user ~A" user)
(process-user user))))
If there is a problem processing users, you might get something in the debugger that looks like this:
Could not find home directory for "nancy" [Condition of type SIMPLE-ERROR] Restarts: 0: [SKIP] Skip user nancy 1: [STOP-PROCESSING] Stop processing users
You can then interactively choose which action to take, depending on what’s most appropriate for the condition at hand.
(You can also choose restarts non-interactively. That’s a tip for another day.)