4

Suppose I have a class called board:

(defclass board ()
 ((blocker    :accessor blocker    :initarg :blocker    :initform  0))

According to this book I can define a custom setf for blocker by:

(defmethod (setf blocker) (new-blocker (b board))
  (setf (slot-value b 'blocker) new-blocker))

However, steel bank common lisp will say function not defined, even though I have evaluated it. Does anyone know what's wrong here?

Mark
  • 8,408
  • 15
  • 57
  • 81
  • 4
    Note: you don't need to define (setf blocker) given that the :accessor keyword already defined one for you. – GoZoner Jun 02 '12 at 20:35

2 Answers2

1

That looks correct. Note that you are redefining the already existing setf method that you created by specifying :accessor blocker. SBCL will give you a style-warning about that.

Your mistake is somewhere else. Are you in a different package, perhaps? Try to show the steps you have taken in your IDE to compile and load those forms, and to attempt to run that method invocation.

Svante
  • 50,694
  • 11
  • 78
  • 122
  • I tried to check if setf-blocker is defined by using (fboundp 'setf-blocker), but it returned nil. And when I tried defining it myself it still complains that the function isn't defined. – Mark Jun 03 '12 at 01:19
  • Works in my env, with latest SBCL and OS X. Your defclass form is missing a closing parenth; is this the problem? Good book though; also check out 'The Art of the MetaObject Protocol' – Clayton Stanley Jun 03 '12 at 06:57
  • @Mark: That function is not called `setf-blocker` but `(setf blocker)`. Show the code you are trying to evaluate. – Svante Jun 03 '12 at 16:21
0

You have to declare a generic function before defining any methods.

(defgeneric (setf blocker) (new-blocker board))

See this chapter in Practical Common Lisp for an example.

Daimrod
  • 4,902
  • 23
  • 25
  • 1
    Incorrect. If no generic function exists, one will be created for you. See http://l1sp.org/7.6.2 for details. – Xach Jun 02 '12 at 22:19
  • Oh you're right, SBCL only emits a STYLE-WARNING. Btw your link doesn't work. ([7.6.2](http://www.lispworks.com/documentation/HyperSpec/Body/07_fb.htm)) – Daimrod Jun 03 '12 at 09:02