I'm new to clojure and macros and am having trouble wrapping my head around
some simple things.
I know that this macro will add 2 to its argument:
user=> (defmacro plus-two [x] `(+ 2 ~x))
#'user/plus-two
user=> (plus-two 5)
7
user=> (def u 10)
#'user/u
user=> (plus-two u)
12
I tried the following just to see what will happen and don't understand
what's going on:
user=> (defmacro foo [x] (+ 2 x))
#'user/foo
user=> (foo 5)
7
user=> (foo u)
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.Number
clojure.lang.Numbers.add (Numbers.java:128)
I tried quoting the plus:
user=> (defmacro bar [x] ('+ 2 x))
#'user/bar
user=> (bar 5)
5
user=> (bar u)
10
This makes sense, since (bar u) is the same as + 2 u (without parentheses),
which returns u. But I don't understand what's happening with foo, and I
can't check it with macroexpand since that gives the same error. Is there a
way to recreate this error without defining a macro?
Thanks in advance for any and all responses.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.