Clojure deftype и протокол
У меня есть этот код Clojure в моем (NS обработчики)
(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
Я читал о вводящем в заблуждение сообщении об ошибке, когда в функцию Protocol передается недопустимое количество аргументов, однако, как вы можете видеть, это не тот случай.
В чем может быть проблема? Какие-либо предложения? Greg
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!")))