Is there a way to get class slots?

I have a class like this

(defclass shape () ((color :initform :black) (thickness :initform 1) (filledp :initform nil) (window :initform nil))) 

Is there a general lisp function, how to get a list of these slots if I only know an instance of this class?

+8
source share
1 answer

Many common Lisp implementations support the CLOS meta-object protocol. This provides introspective operations for classes, slots, and other meta objects.

In LispWorks, the corresponding functions are available directly in the CL-USER package.

 CL-USER 139 > (defclass shape () ((color :initform :black) (thickness :initform 1) (filledp :initform nil) (window :initform nil))) #<STANDARD-CLASS SHAPE 40202910E3> CL-USER 140 > (mapcar #'slot-definition-name (class-direct-slots (class-of (make-instance 'shape)))) (COLOR THICKNESS FILLEDP WINDOW) 

The slot-definition-name and class-direct-slots functions are defined by the CLOS meta-object protocol and are supported in many Common Lisp implementations - only the package in which they are located may differ. For example, in SBCL they can be found in the SB-MOP package.

From the class we can get a list of direct slots. Forward slots are slots that are directly defined for this class and which are not inherited. If you want to get a list of all slots, use the class-slots function.

The slot here means that we get a slot definition object that describes the slot. To get the name of the slot, you must get the name from the slot definition object using the slot-definition-name function.

+13
source

Source: https://habr.com/ru/post/1260137/


All Articles