sheepleでオブジェクトを作る
CLikiのRecent Changesを購読していると、色々なプログラムの更新が分かります。
今日はsheepleというオブジェクトシステムについての記事が更新されていました。
http://www.cliki.net/Sheeple
http://common-lisp.net/project/sheeple/
asdf-installでインストールもできます。
sheepleでのオブジェクトの作りかた
objectもしくはcloneで新しいオブジェクトを作ります。
オブジェクトはpropertyを持ち、property-valueでアクセスできます。また、property-valueはsetf可能です。
propertyはCLOSのslotにあたるものですね。
propertyはCLOSとは異なり、オブジェクト作成後、勝手に追加できます。
propertyに関するメタ情報も取得できます。
available-propertiesでオブジェクトが使用できるpropertyのリストを得られます。
direct-propertiesでそのオブジェクトが直接持っているpropertyのリストを得られます。
;; 点オブジェクトを作る (defvar *point* (object)) (setf (property-value *point* 'x) 10) ;; => 10 (setf (property-value *point* 'y) 20) ;; => 20 (direct-properties *point*) ;; => (Y X) (available-properties *point*) ;; => (Y X SHEEPLE::NICKNAME) ;; 点オブジェクトを元に点2を作る(delegationで) (defvar *point2* (object :parents (list *point*))) (direct-properties *point2*) ;; => NIL (available-properties *point2*) ;; => (Y X SHEEPLE::NICKNAME) ;; property X Yへのアクセスは*point*へdelegateされる (property-value *point2* 'x) ;; => 10 (setf (property-value *point* 'x) 15) ;; => 15 (property-value *point2* 'x) ;; => 15 ;; プロパティを更新(Xのdelegateがshadowされる) (setf (property-value *point2* 'x) 30) ;; => 30 (property-value *point2* 'x) ;; => 30 (property-value *point* 'x) ;; => 15 (direct-properties *point2*) ;; => (X) (available-properties *point2*) ;; => (Y X SHEEPLE::NICKNAME) ;; 点オブジェクトを元に点3を作る(cloneで) (defvar *point3* (clone *point*)) (property-value *point3* 'x) ;; => 15 (property-value *point3* 'y) ;; => 20 (direct-properties *point3*) ;; => (Y X) (available-properties *point3*) ;; => (Y X SHEEPLE::NICKNAME)
with-slotsに似た、with-propertiesもあります。
(with-properties (x y) *point* (list x y)) ;; => (15 20)
ディスパッチ機構は、またの機会に。