Created
October 6, 2010 20:31
-
-
Save jido/614023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Dispatch message to object | |
(defmacro call* [obj, getter & args] | |
`(let [object# ~obj] | |
(((keyword '~getter) @(type object#)) object# ~@args))) | |
(defrecord Dodo [instance]) | |
(def DodoClass (new Dodo nil)) | |
(defrecord SimpleList [values]) | |
(def SimpleListClass | |
(merge DodoClass | |
{ | |
:instance (with-meta (new SimpleList [1 2 3 4]) {:type (var SimpleListClass)}) | |
:values (fn [self] (-> self :values)) | |
})) | |
(println (call* (-> SimpleListClass :instance) values)) | |
(def ExtendedClass | |
(merge SimpleListClass | |
{ | |
:instance (with-meta (new SimpleList [4 5 6]) {:type (var ExtendedClass)}) | |
:shift (fn [self] | |
(with-meta self | |
{:type (ref | |
(merge @(type self) | |
{ | |
:values (fn [self] (subvec (-> self :values) 1)) | |
})) | |
})) | |
})) | |
(def x (call* (-> ExtendedClass :instance) shift)) | |
(println (call* x values)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment