以下是一个有效的Clojure示例( focus on 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元数据是无效的,这可能会引起反射警告,并可能混淆第三方工具。