【发布时间】:2015-01-30 01:38:23
【问题描述】:
我正在尝试创建一个类似于 Quartzite defjob 宏的宏,该宏创建带有 @DisallowConcurrentExecution 注释的 Job 类。代码在 repl 中工作,但不在宏内部。
这行得通...
user=> (defrecord ^{DisallowConcurrentExecution true} YYY []
#_=> org.quartz.Job
#_=> (execute [this context]
#_=> (println "whoosh!")))
user.YYY
user=> (seq (.getAnnotations YYY))
(#<$Proxy3 @org.quartz.DisallowConcurrentExecution()>)
...但事实并非如此。
(defmacro defncjob
[jtype args & body]
`(defrecord ^{DisallowConcurrentExecution true} ~jtype []
org.quartz.Job
(execute [this ~@args]
~@body)))
根据罗德里戈的建议,这里有一种方法可以让它发挥作用。
(defmacro defdcejob
[jtype args & body]
`(defrecord ~(vary-meta jtype assoc `DisallowConcurrentExecution true) []
org.quartz.Job
(execute [this ~@args]
~@body)))
【问题讨论】:
-
你能运行 `(macroexpand-1 '(defncjob XXX [context] (println "whooosh"))) 并包含结果吗? (这是every宏问题的第一步)
-
您不能在宏中使用阅读器宏 (^)。看这里:stackoverflow.com/questions/7754429/…
-
@RodrigoTaboada 请将其放入答案中,以便我接受。谢谢
标签: clojure macros annotations quartzite