3

So If I have a defclass object and I make an instance of it and place it inside an array. How do I get the value of its slots inside the array?

I've tried:

(slot-value (aref *array* 0) :name)

I guess I am just not understanding how to access an object that is inside an array.

I can print the object in an unreadable form using (format t) but is there a way to print an object and all the slots in a form I can actually understand?

(defun generate-object (name)
  (let ((a (make-instance 'person
                          :name name)))
    (setf (aref *array* 0) a)))

it places the object inside the array but it seems that the slot is not being created?

This causes the problem:

(defclass person ()
  ((name :accessor name
         :reader read-name
         :initarg :name)))

(defvar *array* 0)
(setf *array* (make-array 20))

(defun generate-object (name)
  (let ((a (make-instance 'person
                          :name name)))
    (setf (aref *array* 0) a)))
10
  • What is your problem? Commented Mar 10, 2014 at 17:17
  • I need to access the value of a slot inside an object that is inside an array. I don't know how to do it. Commented Mar 10, 2014 at 17:20
  • What's wrong with the code you posted? Commented Mar 10, 2014 at 17:21
  • I get an error that says: when attempting to ~A the slot ~S is missing from the object. but my function creates the class and sets the values of the slots correctly. I'll edit my function into my post. Commented Mar 10, 2014 at 17:27
  • Could you post a complete but minimal version of your code that exhibits the problem? Commented Mar 10, 2014 at 17:40

2 Answers 2

3

The slot name needs to be a symbol that is syntactically valid as a variable name. Try 'name instead of :name .

(slot-value (aref *array* 0) 'name)

Look at the examples here.

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

3 Comments

That's the same syntax as the original: slot-value object slot-name. The important difference is that OP's object didn't have a slot named by the keyword symbol :name, but rather by some other symbol name.
@JoshuaTaylor I have fixed my sloppy wording. Also, according to the standard, it looks like slot names cannot be keyword symbols.
+1 for updating. The answer was definitely what was needed; I was just nitpicking. :) Investigating keyword use is always a good idea. I've always liked that you can use non-keyword symbols as keyword-names for keyword arguments. E.g., (defun foo (&key ((bar baz))) baz) and call (foo 'bar 42).
0

While possible, it is not recommended to use slot-value outside of the low-level class-specific code (like initialize-instance methods etc.).

You should instead add accessors to your slots and use those. For example:

(defclass foo ()
  ((bar :reader foo-bar
        :initarg :bar)))

This defines a class foo with a slot bar. You can initialize the slot upon object instantiation with the :initarg name:

(let ((my-foo (make-instance 'foo :bar "baz")))
  #| whatever |#)

You can read the slot value with the defined :reader:

(let ((my-foo (make-instance 'foo :bar "baz")))
  (foo-bar my-foo))

It doesn't matter where you get your foo from, of course. Imagine you have an array foo-array which is filled with foos. To get the bar slot value of the fourth foo in that array:

(foo-bar (aref foo-array 3))

If you also want to set the value, use an :accessor instead of the :reader slot option.
Then you can use it as a place:

(let ((my-foo (make-instance 'foo)))
  (setf (foo-bar my-foo) "quux"))

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.