Shammer's Philosophy

My private adversaria

CLOSクラス変数インスタンス変数

LispでもJavaと同じようにクラス変数とインスタンス変数があるようだ。Javaではstaticを使った。CLOSでは、allocation で class か instance かを指定するようだ。デフォルトは instance らしい。

? (defclass foo ()
((global-x :reader get-global-x :writer set-global-x :allocation :class)
 (x :reader get-x :writer set-x :allocation :instance)))
#<STANDARD-CLASS FOO>
? (defvar foo1 (make-instance 'foo))
FOO1
? (defvar foo2 (make-instance 'foo))
FOO2
? (get-global-x foo1)
> Error: Slot GLOBAL-X is unbound in #<FOO #x302000DDC7DD>
> While executing: #<CCL::STANDARD-KERNEL-METHOD SLOT-UNBOUND (T T T)>, in process Listener(4).
> Type cmd-. to abort, cmd-\ for a list of available restarts.
> Type :? for other options.
1 > q
? (get-global-x foo2)
> Error: Slot GLOBAL-X is unbound in #<FOO #x302000DE253D>
> While executing: #<CCL::STANDARD-KERNEL-METHOD SLOT-UNBOUND (T T T)>, in process Listener(4).
> Type cmd-. to abort, cmd-\ for a list of available restarts.
> Type :? for other options.
1 > q
? (set-global-x 987654321 foo1)
987654321
? (get-global-x foo1)
987654321
? (get-global-x foo2)
987654321
? (set-x 12345 foo1)
12345
? (get-x foo1)
12345
? (get-x foo2)
> Error: Slot X is unbound in #<FOO #x302000DE253D>
> While executing: #<CCL::STANDARD-KERNEL-METHOD SLOT-UNBOUND (T T T)>, in process Listener(4).
> Type cmd-. to abort, cmd-\ for a list of available restarts.
> Type :? for other options.
1 > q
? (set-x 56789 foo2)
56789
? (get-x foo2)
56789
? (get-x foo1)
12345
?