0

我的(ns 处理程序)中有这个 Clojure 代码

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

the-action-handler是 deftype 处理程序之一的有效实例。当我尝试编译时,出现此错误:

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

当将无效数量的参数传递给协议函数时,我确实读过有关误导性错误消息的信息,但是,如您所见,情况并非如此。

可能是什么问题?有什么建议么?格雷格

4

1 回答 1

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!")))
于 2014-05-15T01:33:19.057 回答