This method is also applicable to all the user defined objects. When a class or a structure is defined, the access and update forms for each slot are automatically defined. Each of those forms is defined as a macro whose name is the concatenation of the class name and slot name. For example, car of a cons can be addressed by (cons-car '(a b c)).
(defclass person :super object :slots (name age)) (defclass programmer :super person :slots (language machine)) (setq x (instantiate programmer)) (setf (programmer-name x) "MATSUI" (person-age x) 30) (incf (programmer-age x)) (programmer-age x) --> 31 (setf (programmer-language x) 'EUSLISP (programmer-machine x) 'SUN4)
Array elements can be accessed in the same manner.
(setq a (make-array '(3 3) :element-type :float)) (setf (aref a 0 0) 1.0 (aref a 1 1) 1.0 (aref a 2 2) 1.0) a --> #2f((1.0 0.0 0.0) (0.0 1.0 0.0) (0.0 0.0 1.0)) (setq b (instantiate bit-vector 10)) --> #*0000000000 (setf (bit b 5) 1) b --> #*0000010000
In order to define special setf methods for particular objects, defsetf macro is provided.
(defsetf symbol-value set) (defsetf get (sym prop) (val) `(putprop ,sym ,val ,prop))
k-okada 2013-05-21