开发者

In clojure how do I require a multimethod?

I know I can do (:use function) but how do I do thi开发者_开发问答s for a multimethod?


Multimethods are used from other namespaces in the same way as functions.

If you have the following in com/example/foo.clj

(ns com.example.foo)

(defn f [x]
  (* x x))

(defmulti m first)

(defmethod m :a [coll]
  (map inc (rest coll)))

In the file com/example/bar.clj you can use both f and m in the same manner:

(ns com.example.bar
  (:use [com.example.foo :only [f m]]))

(defn g []
  (println (f 5)) ; Call the function
  (println (m [:a 1 2 3]))) ; Call the multimethod

;; You can also define new cases for the multimethod defined in foo
;; which are then available everywhere m is
(defmethod m :b [coll]
  (map dec (rest coll)))

I hope this answers your question!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜