以下两个在宏中使用函数的示例会导致评估没有错误。
(defmacro works []
(let [f (fn [] 1)]
`(~f)))
(works)
;; => 1
(defn my-nullary-fn []
(fn [] 2))
(defmacro also-works []
(let [f (my-nullary-fn)]
`(~f)))
(also-works)
;; => 2
然而,
(defmacro does-not-work []
(let [f (constantly 3)]
`(~f)))
(does-not-work)
投掷
java.lang.IllegalArgumentException: No matching ctor found
for class clojure.core$constantly$fn__4051
同样地,
(defn my-unary-fn [x]
(fn [] x))
(defmacro also-does-not-work []
(let [f (my-unary-fn 4)]
`(~f)))
(also-does-not-work)
投掷
java.lang.IllegalArgumentException No matching ctor found
for class user$my_other_fn$fn__12802
可能是什么原因?fn
、my-nullary-fn
和所返回的函数对象之间constantly
有区别my-unary-fn
吗?
我正在运行 Clojure 1.5.1。
CLJ-946可能与此有关。