我在 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 并未显示。这特别令人惊讶,因为 (extends? IGreeter MyGreeter) 返回 true。我是不是理解错了,或者是 bug 吗?谢谢!