2014-05-17 3 views
0

Clojure에서 multimethod를 예상대로 작동 시키는데 어려움을 겪고 있습니다. 내 코드의 증류는 다음과 같습니다. 하나의 첫 번째 defmethod로 보내 대신 내가 널 포인터 예외를 얻어야한다 :예기치 않은 null 포인터를주는 Clojure multimethod

(defn commandType [_ command] (:command-type command)) 

(defmulti testMulti commandType) 

(defmethod testMulti :one [game command] (str "blah")) 

(defmethod testMulti :default [& args] "Cannot understand") 

(testMulti "something" {:command-type :one}) 

(commandType "something" {:command-type :one}) 

지금은 물론 수익의 것 인수에 호출 된 메소드의 CommandType을가 여기에 기대. here 잘 작동있는 Clojure의 워드 프로세서에서

(defmulti simpleMulti :key) 

(defmethod simpleMulti "basic" [params] "basic value") 

(simpleMulti {:key "basic"}) 

그리고 아직 예 : 심지어 multimethod의 간단한 호출 나는 나에게 널 포인터를 제공합니다 함께 올 수 있습니다. 내가 잘못하고있는 근본적인 것이 있습니까?

답변

1

지금까지 보았 듯이 작동합니다.

(defmulti testMulti (fn [_ command] (:command-type command))) 

(defmethod testMulti :one [game command] (str "blah")) 

(defmethod testMulti :default [& args] "Cannot understand") 

(testMulti "something" {:command-type :one}) 
; "blah" 

(testMulti "something" {:command-type :two}) 
; "Cannot understand" 

(testMulti "something" 5) 
; "Cannot understand" 

예상 주어진.

위에서 실행하기 전에 REPL을 재설정했습니다.

간단한 예제도 사용할 수 있습니다. 다음

(simpleMulti {:key "basic"}) 
; "basic value" 
+0

네 말이 맞아

(defmulti simpleMulti :key) (defmethod simpleMulti "basic" [params] "basic value") 

을 감안할 때. 그들은 새로운 REPL을 시작한 후에 일했습니다. –