在clojure中,宏为程序员提供了巨大的力量。 eval
也是非常强大的。两者之间存在一些细微的差异。我希望这个谜题能对这个话题有所启发。Clojure谜题:eval,宏和命名空间
(ns hello)
(defmacro my-eval [x] `~(read-string x))
(defn hello[] "Hello")
(defn run-stuff []
(println (hello))
(println (my-eval "(hello)"))
(println (eval (read-string "(hello)"))))
(ns main)
(try (hello/run-stuff)
(catch Exception e (println e)))
在里面run-stuff
体的3个语句,其中一个导致异常,为什么其他的人不?
在调查这个美丽的问题Clojure - (read-string String calling function后,我制定了以下谜语。感谢@Matthias Benkard的澄清