1

This code creates an error: The slot COMMON-LISP-USER::NAME is unbound in the object #<THING {1002B830F3}>. I tried sbcl and clisp, both creating same problem. I can initialize instance of ash manually, but when done in "when" statement, it doesn't work

(defclass thing ()
  ((name :initarg :name :accessor thing-name)
   (properties :initform nil :accessor thing-properties)))

(defmethod initialize-instance :around ((obj thing) &rest initargs &key &allow-other-keys)
  (let* ((class (class-of obj)))
    (apply #'call-next-method obj
           (loop for (key val) on initargs by #'cddr
                 when (member key '(name properties))  ; Explicitly allow these
                 append (list key val)))))

(defclass process () 
  ((name :initarg :name :accessor process-name)))

(defmethod burn ((proc process) (chair thing) (oxygen thing))
  (declare (ignore proc))
  (when (and (string= (thing-name chair) "chair")
             (string= (thing-name oxygen) "oxygen"))
    (make-instance 'thing :name "ash")))

;; Test
(defparameter *chair* (make-instance 'thing :name "chair"))
(defparameter *oxygen* (make-instance 'thing :name "oxygen"))
(defparameter *burning* (make-instance 'process :name "burning"))

;; Perform the burn operation
(defparameter *result* (burn *burning* *chair* *oxygen*))

;; Debugging output
(format t "Result: ~A" *result*)  ; Should show the created "ash" instance if the burn succeeds

1 Answer 1

3

The error is inside this method:

(defmethod initialize-instance :around ((obj thing) &rest initargs &key &allow-other-keys)
    (let* ((class (class-of obj)))
    (apply #'call-next-method obj
        (loop for (key val) on initargs by #'cddr
            when (member key '(name properties))  ; Explicitly allow these
                append (list key val)))))

You should use list of keyword symbols, not list of non-keyword symbols: (member key '(:name :properties))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.