我正在尝试编写一个将生成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)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
java.lang.ClassCastException: clojure.lang.Cons cannot be cast to java.lang.String
Run Code Online (Sandbox Code Playgroud)
而且我不确定这意味着什么,但我有这个模糊的概念(因为...)没有像我认为的那样在宏内部工作.
clojure ×1