2011-04-20 13 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))) 

我希望這能回答你的問題!