【问题标题】:Clojure deftype and protocolClojure deftype 和协议
【发布时间】:2014-05-15 00:13:21
【问题描述】:

我的(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

当无效数量的参数被传递到协议函数时,我确实读到了误导性错误消息,但是,正如您所见,情况并非如此。

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

【问题讨论】:

    标签: clojure clojure-protocol


    【解决方案1】:

    我相信您传递的是两个参数而不是一个。发生的事情是协议方法的第一个参数是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!")))
    

    【讨论】:

    • 谢谢。这正是我遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多