Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a class object stored in an array?

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)))
like image 285
Cody Mallery Avatar asked Feb 03 '26 03:02

Cody Mallery


1 Answers

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.

like image 90
sigjuice Avatar answered Feb 06 '26 01:02

sigjuice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!