I aggravated the infos by @RainerJoswig which he gave to similar questions in this answer and this. Credits to him!
CAVE:
This answer works in clisp and sbcl. Other implementations I didn't test.
According to Rainer's answer, it should also work in Clozure CL.
Should you encounter problems in different implementations,
all you have to do is to check via (apropos 'slot-definition-name)
and (apropos 'class-slots)
btw. (apropos 'class-direct-slots)
in which package/namespace these key functions for this all are and add a #+<implementationname>
clause for the specific implementations into the definitions as I did here.
slot-definition-name
, class-slots
btw class-direct-slots
are functions of the meta object protocol (MOP) of CLOS. Where they are available is implementation specific.
(defun slot-doc (class slot)
(let* ((slots #-sbcl(class-slots (find-class class))
#+sbcl(sb-mop:class-direct-slots (find-class class)))
(i (position slot #-sbcl(mapcar #'slot-definition-name slots)
#+sbcl(mapcar #'sb-mop:slot-definition-name slots)))
(slot-obj (elt slots i)))
(documentation slot-obj t)))
;; usage:
(defclass class1 () ((slot1 :documentation "doc")))
(slot-doc 'class1 'slot1)
;; "doc"
So, for example in sbcl, you can get documentation for all slots with:
(mapcar (lambda (s) (documentation s t)) (sb-mop:class-direct-slots (find-class 'class1))
sb-mop:class-slots
did sth different in sbcl. Seems to be a "false-friend" for class-slots
.
To not to have to quote the class and slot symbols,
you can make a macro out of it:
(defmacro slot-documentation (class slot)
`(let* ((slots #-sbcl(class-slots (find-class ',class))
#+sbcl(sb-mop:class-direct-slots (find-class ',class)))
(i (position ',slot #-sbcl(mapcar #'slot-definition-name slots)
#+sbcl(mapcar #'sb-mop:slot-definition-name slots)))
(slot-obj (elt slots i)))
(documentation slot-obj t)))
;; usage:
(defclass class1 () ((slot1 :documentation "doc")))
(slot-documentation class1 slot1)
;; "doc"