Is there a way to gather slot-definition-readers from all the inheritance tree?
Asked Answered
P

1

4

The generic function slot-definition-readers gets an argument that must be a direct-slot-definition. If an object is an instance of a class that inherits from another class how can I get hold of the readers of all the effective-slots of the object? Do I manually have to traverse the tree and call slot-definition-readers on the result of class-direct-slots in each superclass, gathering the results, or is there another way that I am not aware of?

Placoid answered 19/7, 2016 at 7:43 Comment(1)
I haven't seen such a thing in a library. It might exist. But for know it looks like you have to write it similar how you have described it.Disabuse
S
4

This "community wiki" answer is here to provide an implementation of this feature. What follows uses no destructive operation (NCONC, MAPCAN) since an implementation might return an internal list without copying it. MAPPEND is imported from alexandria, and MOP operations can be imported from closer-mop.

(defun all-direct-slots (class)
  (append (class-direct-slots class)
          (mappend #'all-direct-slots
                   (class-direct-superclasses class))))

(defun all-slot-readers (class)
  (mappend #'slot-definition-readers
           (all-direct-slots class)))
Symposium answered 19/7, 2016 at 7:43 Comment(2)
SLOT-DEFINITION-READERS seems to return the actual list of readers (rather than a copy) on SBCL, so using it with mapcan kind of breaks things (try calling it twice on a subclass that both has a reader and inherits another). You should compose it with COPY-LIST to avoid problems.Parous
@Parous Thanks a lotSymposium

© 2022 - 2024 — McMap. All rights reserved.