【发布时间】:2011-12-12 17:36:38
【问题描述】:
我正在尝试编写一个将生成 n 个函数的宏。这是我目前所拥有的:
; only defined this because if I inline this into make-placeholders
; it's unable to expand i# in ~(symbol (str "_" i#))
(defmacro defn-from [str mdata args & body]
`(defn ~(symbol str) ~mdata ~args ~@body))
; use list comprehension to generate n functions
(defmacro make-placeholders [n]
`(for [i# (range 0 ~n)] (defn-from (str "_" i#) {:placeholder true} [& args] (nth args i#))))
; expand functions _0 ... _9
(make-placeholders 9)
我得到的错误是:
java.lang.ClassCastException: clojure.lang.Cons cannot be cast to java.lang.String
我不太确定这意味着什么,但我有一个模糊的概念,即 (for ...) 并没有像我认为的那样在宏中工作。
【问题讨论】:
标签: clojure