2011-04-20 5 views

답변

7

멀티 메소드는 함수와 같은 방식으로 다른 네임 스페이스에서 사용됩니다.

당신은 COM에 다음과 같은 경우/예/foo.clj 파일 COM/예에서

(ns com.example.foo) 

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

(defmulti m first) 

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

/당신은 같은 방법으로 F와 m를 모두 사용할 수 있습니다 bar.clj :

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

이 질문에 대한 답변을 보내주십시오.

관련 문제