Frage

I have this Clojure code in my (ns handlers)

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

In my core.clj I got this code (omitting few lines and namespace stuff):

    (handle the-action-handler params session)

the-action-handler is valid instance of one of deftype Handlers. When I try to compile I get this error:

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

I did read about misleading error message when invalid number of arguments is passed into Protocol function, however, this is not the case as you can see.

What might be the problem? Any suggestions? Greg

War es hilfreich?

Lösung

I believe you are passing two params instead of one. What is happening is the first parameter to the protocol method is the this parameter.

Try this instead

(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!")))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top