> I know that this macro will add 2 to its argument:

This isn't what that macro does. That macro takes a piece of syntax, and
sticks that syntax in the hole in the expression (+ 2 HOLE). The
macro doesn't evaluate the expression, it just generates it. It is the
evaluation of *that* expression that yields the plus-two result.

The arguments to a macro are not values, they are syntax. The result of a
macro isn't a value, it's syntax. When you call (foo u), you are not
passing the value of u into the macro. You are passing in the syntactic
symbol u. That's why it says you can't add a Symbol to a Number. Your macro
is trying to compute (at "compile time") (+ 2 'u).

On Tuesday, January 26, 2016, <echigoyamont...@gmail.com> wrote:

> 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 clojure@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure@googlegroups.com');>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>
> 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 clojure+unsubscr...@googlegroups.com
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
-- Dan Burton

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to