2011-04-20 14 views

回答

7

多方法以与函数相同的方式从其他名称空间中使用。

如果已中COM以下/示例/ foo.clj

(ns com.example.foo) 

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

(defmulti m first) 

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

在文件COM /示例/ bar.clj可以以同样的方式使用这两种f和米:

(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))) 

我希望这能回答你的问题!