Skip to content

Instantly share code, notes, and snippets.

@jido
Created October 6, 2010 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jido/614023 to your computer and use it in GitHub Desktop.
Save jido/614023 to your computer and use it in GitHub Desktop.
;; 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