此代码来自Clojure的行动清单3.5。当我尝试运行它时,我收到以下错误:
无法动态绑定非动态var:joy.q /两次
自从这本书被印刷以来,我做错了什么还是Clojure改变了约束规则?
(defn twice [x]
(println "original function")
(* 2 x))
(defn call-twice [y]
(twice y))
(defn with-log [function-to-call log-statement]
(fn [& args]
(println log-statement)
(apply function-to-call args)))
(call-twice 10)
(binding [twice (with-log twice "Calling the twice function")]
(call-twice 20))
答案 0 :(得分:3)
来自binding文档:
从Clojure 1.3开始,vars需要明确标记为^:dynamic才能 它们是动态可重新绑定的
所以你需要:
(defn ^:dynamic twice [x]
(println "original function")
(* 2 x))