2014-05-15 91 views
0

我在我的(NS處理程序)這個Clojure的代碼Clojure的DEFTYPE和協議

(defprotocol ActionHandler 
    (handle [params session])) 

(defrecord Response [status headers body]) 

(deftype AHandler [] 
    ActionHandler 
    (handle [params session] 
      (Response. 200 {"Content-Type" "text/plain"} "Yuppi, a-handler works"))) 


(deftype BHandler [] 
    ActionHandler 
    (handle [params session] 
      (Response. 200 {"Content-Type" "text/plain"} "YES, the b-handler is ON"))) 

(deftype CHandler [] 
    ActionHandler 
    (handle [params session] 
      (Response. 200 {"Content-Type" "text/plain"} "C is GOOD, it's GOOD!"))) 

在我core.clj我得到這個代碼(省略幾行和命名空間的東西):

(handle the-action-handler params session) 

動作處理程序是deftype處理程序之一的有效實例。 當我嘗試編譯我得到這個錯誤:

java.lang.IllegalArgumentException: No single method: handle of interface: handlers.ActionHandler found for function: handle of protocol: ActionHandler 

我沒有讀到時的參數數目無效傳遞到協議功能誤導性的錯誤消息,但是,這並不像你所看到的情況。

可能是什麼問題?有什麼建議麼? Greg

回答

0

我相信你是通過兩個參數而不是一個。發生什麼事是協議方法的第一個參數是this參數。

試試這個

(defprotocol ActionHandler 
    (handle [this params session])) 

(defrecord Response [status headers body]) 

(deftype AHandler [] 
    ActionHandler 
    (handle [this params session] 
      (Response. 200 {"Content-Type" "text/plain"} "Yuppi, a-handler works"))) 


(deftype BHandler [] 
    ActionHandler 
    (handle [this params session] 
      (Response. 200 {"Content-Type" "text/plain"} "YES, the b-handler is ON"))) 

(deftype CHandler [] 
    ActionHandler 
    (handle [this params session] 
      (Response. 200 {"Content-Type" "text/plain"} "C is GOOD, it's GOOD!"))) 
+0

感謝。這正是我遇到的問題。 – greggigon