When unchecked-add is given an invalid argument, sometimes it throws
an exception, and sometimes it does something very weird -- it returns
the code for the call!
Referring to the files foo1.clj and foo2.clj below, if I type at the
REPL
(use 'com.ksvanhorn.foo2)
I get "java.lang.IllegalArgumentException: No matching method found:
unchecked_add (foo2.clj:0)", which is unsurprising, as (* magic x) is
much too large to fit into an int.
However, if I type
(use 'com.ksvanhorn.foo1)
I get "v: (clojure.core/unchecked-add 1 9948446125717)" printed out
and nil returned, with no exception thrown.
The only difference between foo1.clj and foo2.clj is that foo1.clj
uses a macro defined within a let. If I replace the macro call with
its expansion, or if I define the macro outside of the let, then I get
the same behavior as for foo2.clj.
--- BEGIN foo1.clj ---
(ns com.ksvanhorn.foo1)
(let [dummy 0]
(defmacro add [& args] `(unchecked-add ~...@args))
(let [magic 1812433253
x 5489
v (add 1 (* magic x))]
(println (str "v: " v))))
--- END ---
--- BEGIN foo2.clj ---
(ns com.ksvanhorn.foo2)
(let [magic 1812433253
x 5489
v (unchecked-add 1 (* magic x))]
(println (str "v: " v))))
--- END ---
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---