我知道我可以使用(:use function)
,但是如何在多方法中执行此操作呢?
多方法与函数的使用方式相同,可以从其他命名空间中调用。
如果你在com/example/foo.clj文件中有以下内容
(ns com.example.foo)
(defn f [x]
(* x x))
(defmulti m first)
(defmethod m :a [coll]
(map inc (rest coll)))
(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)))