【问题标题】:Attempt to add annotation to defrecord defined class in macro尝试在宏中为 defrecord 定义的类添加注释
【发布时间】: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


【解决方案1】:

您不能在宏内部使用 ^ reader macro。看看this类似的问题。

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多