我遇到了这个问题......我有两个看起来非常相似的宏
(import java.lang.management.ManagementFactory)
(defmacro with-thread-manager [tm & body]
{:pre [(and (vector? tm) (= 1 (count tm)))]}
`(let [~(first tm) (ManagementFactory/getThreadMXBean)]
~@body))
(defmacro with-os-manager [tm & body]
{:pre [(and (vector? tm) (= 1 (count tm)))]}
`(let [~(first tm) (ManagementFactory/getOperatingSystemMXBean)]
~@body))
它们的用法如下:
(defn thread-count []
(with-thread-manager [tm] (.getThreadCount tm)))
(thread-count)
;; => 12
(defn application-cpu-time []
(with-os-manager [osm] (.getProcessCpuTime osm)))
(application-cpu-time)
;; => 71260000000
我希望将两者概括with-*-manager
为另一个宏,以便可以像这样简化它们:
(defmanagementblock with-thread-manager (ManagementFactory/getThreadMXBean))
(defmanagementblock with-os-manager (ManagementFactory/getOperatingSystemMXBean))
所以我知道的最简单的方法是稍微改变一下宏
(defmacro with-thread-manager [tm & body]
{:pre [(and (vector? tm) (= 1 (count tm)))]}
(apply list 'let [(first tm) '(ManagementFactory/getThreadMXBean)]
body))
并编写块:
(defmacro defmanageblock [name MANAGER]
(list 'defmacro name '[tm & body]
'{:pre [(and (vector? tm) (= 1 (count tm)))]}
(list 'apply 'list ''let (vector '(first tm) 'MANAGER)
'body)))
一切顺利,除了经理没有正确报价。我尝试了一堆引用和取消引用选项'
,例如 `~'
以及它的许多其他变体。但它没有给出正确的值。