2

When I try to re-define a package in SBCL in such a way that causes name conflicts, I get a NAME-CONFLICT error with the restarts

 0: [KEEP-OLD] Keep symbols already accessible FOO (shadowing others).
 1: [TAKE-NEW] Make newly exposed symbols accessible in FOO, uninterning old ones.
 2: [RESOLVE-CONFLICT] Resolve conflict.
 3: [RETRY] Retry SLIME REPL evaluation request.
 4: [*ABORT] Return to SLIME's top level.
 5: [ABORT] Abort thread (#<THREAD "new-repl-thread" RUNNING {10060E47B3}>)

I'd like to write something that would automatically invoke the TAKE-NEW restart, so that I could do something like

(force (defpackage :foo (:use :cl :bar :baz :mumble)))

The result of this should be the same as calling defpackage, followed by manually invoking the TAKE-NEW restart. The problem is,

CL-USER> (handler-case 
         (defpackage :foo (:use :cl :bar :baz :mumble))
       (error (e) (compute-restarts e)))
(#<RESTART SWANK::RETRY {1006DC40F3}> #<RESTART ABORT {10068007E3}>
 #<RESTART ABORT {10060C7F93}>)
CL-USER> 

I don't seem to have access to that particular restart. As confirmed when I try to invoke it:

CL-USER> (handler-case 
         (defpackage :foo (:use :cl :bar :baz :mumble))
       (name-conflict (e) (invoke-restart 'take-new)))

No restart TAKE-NEW is active.
   [Condition of type SB-INT:SIMPLE-CONTROL-ERROR]

Restarts:
 0: [RETRY] Retry SLIME REPL evaluation request.
 1: [*ABORT] Return to SLIME's top level.
 2: [ABORT] Abort thread (#<THREAD "new-repl-thread" RUNNING {10060E47B3}>)

Any ideas?

1 Answer 1

3

handler-case exits the scope of the restarts before it transfers control to a handler; you need handler-bind. The two other differences between the constructs are that handler-bind has a different syntax (demonstrated below), and handler-bind doesn't transfer control -- that is, for a handler-bind handler to return a value it needs to explicitly transfer control, e.g. with return-from or invoke-restart.

Something like the following should work:

(handler-bind ((sb-ext:name-conflict
                (lambda (c)
                  (when (find-restart 'sb-impl::take-new c)
                    (invoke-restart 'sb-impl::take-new)))))
  (defpackage :foo (:use :mumble)))
2
  • This works. Dammit, I need to read more about condition handling in CL. Cursory search gets me this, this and (of course) this.
    – Inaimathi
    Sep 10, 2014 at 20:51
  • 1
    @Inaimathi the section "Handling in the Context of the Signaler" of this Kent Pitman paper seems to be a nice elaboration of the point at issue here. Glancing at the CLTL2 link, much of its condition discussion seems to be in chapter 29 (also by Pitman).
    – m-n
    Sep 11, 2014 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.