在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的澄清