Here's a quickly hacked together solution (code golf welcome), I'm using
special variables so that we don't affect what other multimethods see:
(def *super* nil)
(defmacro super [afn obj & args]
(let [fn-key (keyword (str afn))]
`(binding [*super* {~fn-key (or (and (~fn-key *super*)
(first (parents (~fn-key *super*))))
(first (parents (type ~obj))))}]
~(if args
`(apply ~afn [~obj ~...@args])
`(apply ~afn [~obj])))))
(defn dispatch-for [fn-sym]
(let [fn-key (keyword (str fn-sym))]
(fn [x]
(if-let [super-type (fn-key *super*)]
super-type
(type x)))))
(defmulti fnA (dispatch-for 'fnA))
(defmethod fnA ::typeA
[x]
(println "Calling fnA, the type of x is" (type x))
(fnB x))
(defmethod fnA ::typeB
[x]
(println "Calling super")
(super fnA x))
(defmulti fnB type)
(defmethod fnB ::typeA
[x]
(println "Calling fnB the type of x is" (type x)))
(derive ::typeB ::typeA)
(fnA (with-meta {} {:type ::typeA}))
(fnA (with-meta {} {:type ::typeB}))
---------------> Output
Calling fnA, the type of x is :user/typeA
Calling fnB the type of x is :user/typeA
Called super
Calling fnA, the type of x is :user/typeB
Calling fnB the type of x is :user/typeB
Cheers,
David
On Tue, Mar 24, 2009 at 2:41 PM, CuppoJava <[email protected]>wrote:
>
> *bump*
>
> Just letting people know that this is still an important and
> unresolved issue for me.
>
> Calling super multi-methods?
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---