我在REPL中玩协议时遇到了以下情况
(defprotocol IGreeter
(greet [this]))
(deftype MyGreeter []
IGreeter
(greet [_] "hi from MyGreeter"))
(extends? IGreeter MyGreeter)
;=> true
;; I expected to see MyGreeter listed here, but result is nil
(extenders IGreeter)
;=> nil
将协议扩展到几个类型。
(extend-protocol IGreeter
String
(greet [this] "hi-string")
clojure.lang.Symbol
(greet [this] 'hi-sym) ; ' help syntax highlighter
clojure.lang.Keyword
(greet [this] :hi-kw))
(extenders IGreeter)
;=> (java.lang.String clojure.lang.Symbol clojure.lang.Keyword)
String、Symbol和Keyword出现在IGreeter的扩展器中。MyGreeter没有出现。这特别令人惊讶,因为(exists? IGreeter MyGreeter)返回true。我是理解错了,还是可能是一个bug?谢谢!