From: Peter Seibel
Subject: FIND-RESTART's condition argument
Date: 
Message-ID: <m3n02rfsu0.fsf@javamonkey.com>
I've seen it said that one should pretty much always pass the optional
condition argument to FIND-RESTART but have never quite been able to
wrap my head around why it really matters. I get what it does in terms
of what restarts it considers--what I don't get is what bad thing
would happen if one always omitted the condition argument. Does anyone
have a simple example of where things go badly with that policy?

Here's the nearest I can come. First define this function:

  (defun foo ()
    (if ok 
      (one-thing)
      (restart-case (error 'my-error)
        (my-restart () (another-thing)))))

So this restart will be associated with the error signaled, thanks to
RESTART-CASE's magic treatment of expressions whose CAR is SIGNAL,
ERROR, WARN, or CERROR.

So now some condition handler handles MY-ERROR by signalling a new
condition:

  (defun medium-level ()
    (handler-bind ((my-error #'(lambda (c) (error 'another-error))))
      (foo)))

Then a condition handler for ANOTHER-ERROR does this:

  (defun high-level ()
    (handler-bind ((another-error
                    #'(lambda (c)
                        (let ((restart (find-restart 'my-restart)))
                          (when restart (invoke-restart restart))))))
      (medium-level)))

Because we didn't pass the condition to FIND-RESTART it will find the
MY-RESTART restart bound in FOO where it wouldn't have if we had
passed the condition object. But if the MY-ERROR condition handler had been:
                                 
  (defun medium-level ()
    (handler-bind ((my-error
                   #'(lambda (c) 
                       (restart-case (error 'another-error)
                         (my-restart () (something))))))
      (foo)))

i.e. there is now a restart associated with the ANOTHER-ERROR, then
FIND-RESTART is going to find it regardless of whether it is called
with the condition object or not since the condition handler is lower
on the call stack than FOO.

So, as I said, I can't figure out why it would ever matter. I think
part of my difficulty comes from the assumption that many interesting
restarts are going to bound in a different place than where the
condition is actually signaled so will not be associated with any
condition unless you go to great lengths to use
WITH-CONDITION-RESTARTS to establish the association. Any pointers?

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp