以下是有效的 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
的:标签元数据时,它似乎具有某种不寻常的形式(因为它仍然是一连串的调用,而不是类字面量)。
(-> (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))}
所以,:标签看起来像一个函数调用。编译器最终是否会调用 (class (log-window-proxy ...)
否则,我会担心这个:标签元数据是无效的,这可能会导致反射警告,并且还会使第三方工具更加困惑。