Hi Cameron,

On Jul 7, 9:49 am, Cameron <cpuls...@gmail.com> wrote:
> Hello all! Today, I've either discovered a bug, or I've discovered a
> flaw in my understanding of macros. Most likely the latter :-) Could
> anyone set me straight?
>
> While this is not the macro I was trying to write, it falls over in
> the same place.
>
> (user=> (defmacro foo [& xs] `(map identity ~xs))
> #'user/foo
> user=> (foo 1 2)
> java.lang.ClassCastException: java.lang.Integer cannot be cast to
> clojure.lang.IFn (NO_SOURCE_FILE:0)

Well, let's look at the macroexpansion:

user> (macroexpand '(foo 1 2))
(clojure.core/map clojure.core/identity (1 2))

As you can see, this tries to call 1 with an argument of 2, which
gives the exception you see.

If you want your arguments evaluated, I think your solution is as good
as any.  If you want them unevaluated, you can do:

user> (defmacro foo2 [& xs] `(map identity '~xs))
#'user/foo2
user> (foo2 1 2)
(1 2)

The difference:

user> (foo2 1 (+ 1 1))
(1 (+ 1 1))

-Jason

-- 
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

Reply via email to