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!
精彩评论