combining methods

14 views
Skip to first unread message

Andy Chambers

unread,
Aug 7, 2009, 2:55:19 PM8/7/09
to Clojure
Hey All,

Does clojure have an equivalent of either CLOS's `call-next-method' or
java's super?

For example, given the multi-method, and the interfaces ICDISCElement
and IODMDef, where IODMDef extends ICDISCElement....

(defmulti validate class)

(defmethod validate ICDISCElement [elem]
;;generic validation of all elements)

(defmethod validate IODMDef [def]
;;how can I do stuff in here then pass control to the other validate
method)

Stuart Sierra

unread,
Aug 7, 2009, 9:37:39 PM8/7/09
to Clojure
There isn't an equivalent right now. The simplest workaround is to
factor out the common code into an ordinary function, and call it from
your multimethods.

-SS


On Aug 7, 2:55 pm, Andy Chambers <achambers.h...@googlemail.com>
wrote:

Meikel Brandmeyer

unread,
Aug 8, 2009, 6:02:24 AM8/8/09
to clo...@googlegroups.com
Hi,

Am 07.08.2009 um 20:55 schrieb Andy Chambers:

> Does clojure have an equivalent of either CLOS's `call-next-method' or
> java's super?


You can use get-method.

(derive ::Foo ::Bar)
(derive ::Foo ::Frob)

(defmulti do-something
type)

(defmethod do-something ::Bar
[x]
(do-bar-stuff x))

(defmethod do-something ::Frob
[x]
(frob x))

(defmethod do-something ::Foo
[x]
(let [frobed-x ((get-method do-something ::Frob) x)
bared-x ((get-method do-something ::Bar) x)]
(do-foo-things frobed-x bared-x)))

Note, that the retrieved method, does not need be in
any way related with the hierarchy. Even if ::Foo is not
derived from ::Bar you could retrieve the ::Bar method
and pass the arguments to it.

Hope this helps.

Sincerely
Meikel

Reply all
Reply to author
Forward
0 new messages