kyle_burton ([info]kyle_burton) wrote,
@ 2008-04-25 10:22:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:lisp

Some Reflection in CLOS

CLOS and slot (member) lookup recently came across c.l.l.

;; some reflection in clos

(defclass test-class
  ()
  ((s1 :reader :slot1
       :writer :set-slot1)
   (s2 :reader :slot2
       :writer :set-slot2)
   (s3 :reader :third-slot
       :writer :update-three)))


(defvar x nil)
(setf x (make-instance 'test-class))

(use-package :clos)

(class-direct-slots (class-of x))
;; =< 
;; (#<standard-direct-slot-definition s1="S1" #x19f471c9="#x19F471C9">
;;  #<standard-direct-slot-definition s2="S2" #x19f471f5="#x19F471F5">
;;  #<standard-direct-slot-definition s3="S3" #x19f47221="#x19F47221">)


(class-direct-superclasses (class-of x))

;; => 
;; (#<standard-class standard-object="STANDARD-OBJECT">)

(mapcar #'slot-definition-readers (class-direct-slots (class-of x)))
;; => ((:SLOT1) (:SLOT2) (:THIRD-SLOT))

(mapcar #'slot-definition-writers (class-direct-slots (class-of x)))
;; => ((:SET-SLOT1) (:SET-SLOT2) (:UPDATE-THREE))




Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…