以下是可以接受的Clojure代码(关注extend-type (class (log-window-proxy nil))
,而不是引用类字面量)
(defn- log-window-proxy [state]
(proxy [javax.swing.JTextArea clojure.lang.IDeref] []
(deref [] state)
(scrollRectToVisible [rect]
(if @(:auto-scroll? state)
(proxy-super scrollRectToVisible rect)))))
(defprotocol LogWindow
(log [this message])
(clear [this]))
(extend-type (class (log-window-proxy nil))
LogWindow
(log [this message]
(println "message" message))
(clear [this]
nil))
然而,当检查给定的this
的:tag
元数据时,它似乎有一种奇特的形状(因为它仍然是一系列调用的链,而不是类字面量)
(-> (extend-type (class (log-window-proxy nil))
LogWindow
(log [this message]
(println "message" message))
(clear [this]
nil)) quote macroexpand last :log second ffirst meta)
;; => {:tag (class (log-window-proxy nil))}
所以,看起来:tag
就像一个函数调用。编译器是否最终会执行(class (log-window-proxy ...
以获得它可以实际用作类型提示的类?
否则,我会担心这个:tag
元数据无效,这可能会引起反射警告,并且可能会使第三方工具困惑。