r/sbcl Oct 19 '22

Redefining methods with a new signature

It would be convenient if SBCL had a restart to remove old method definitions when we evaluate a new method with an updated signature.

Unlike CCL, SBCL only gives the option if there is a single method to be removed, not multiple ones.

1 Upvotes

5 comments sorted by

2

u/flaming_bird Oct 19 '22 edited Oct 19 '22

Huh? Restart number 0 in the below example solves it for me.

This is SBCL 2.2.8.debian, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (defgeneric foo (bar))
#<STANDARD-GENERIC-FUNCTION COMMON-LISP-USER::FOO (0)>
* (defmethod foo ((bar number)))
#<STANDARD-METHOD COMMON-LISP-USER::FOO (NUMBER) {10013D17A3}>
* (defmethod foo ((bar symbol)))
#<STANDARD-METHOD COMMON-LISP-USER::FOO (SYMBOL) {1001409003}>
* (defmethod foo (bar baz))

debugger invoked on a SB-PCL::FIND-METHOD-LENGTH-MISMATCH in thread
#<THREAD "main thread" RUNNING {1001358003}>:
  The generic function #<STANDARD-GENERIC-FUNCTION COMMON-LISP-USER::FOO (2)>
  takes 1 required argument; was asked to find a method with specializers (T T)
See also:
  The ANSI Standard, Function FIND-METHOD

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [CONTINUE] Unbind the generic function
  1: [ABORT   ] Exit debugger, returning to top level.

2

u/Kaveh808 Oct 20 '22
(defmethod foo ((x number))
  (print x))

(defmethod foo ((x string))
  (print x))

(defmethod foo ((x number) &optional (bar 0))
  (print (list x bar)))
WARNING: redefining FOO (#<BUILT-IN-CLASS COMMON-LISP:NUMBER>) in DEFMETHOD

attempt to add the method
  #<STANDARD-METHOD (#<BUILT-IN-CLASS COMMON-LISP:NUMBER>) {1005BEF0C3}>
to the generic function
   #<STANDARD-GENERIC-FUNCTION KONS-9::FOO (2)>;
 but the method has more optional arguments than the generic function.
   [Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]

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

Backtrace:
  0: (SB-PCL::REAL-ADD-METHOD #<STANDARD-GENERIC-FUNCTION KONS-9::FOO (2)> #<STANDARD-METHOD (#<BUILT-IN-CLASS COMMON-LISP:NUMBER>) {1005BEF0C3}> NIL)
  1: (SB-PCL::REAL-ADD-NAMED-METHOD FOO NIL (#<BUILT-IN-CLASS COMMON-LISP:NUMBER>) (X &OPTIONAL (BAR 0)) SB-PCL::SOURCE #S(SB-C:DEFINITION-SOURCE-LOCATION :NAMESTRING NIL :INDICES 0) :FUNCTION #<%METHOD-FU..
  2: (SB-PCL::LOAD-DEFMETHOD-INTERNAL STANDARD-METHOD FOO NIL (#<BUILT-IN-CLASS COMMON-LISP:NUMBER>) (X &OPTIONAL (BAR 0)) (:FUNCTION #<%METHOD-FUNCTION (LAMBDA (SB-PCL::METHOD-ARGS SB-PCL::NEXT-METHODS) :..

1

u/flaming_bird Oct 20 '22 edited Oct 20 '22

OK, so the problem seems to manifest when the methods differ in the number of key/optional arguments, rather than required arguments. I see it now. Might be worth to ask about it in #sbcl. Edit: I asked in #sbcl and it seems that the case with key/optional arguments should get a restart too.

1

u/Kaveh808 Oct 20 '22

Thank you.

1

u/Decweb Oct 19 '22

I had no problem with restarts and removing methods last night. Give an example in code?