On Jan 3, 7:22 pm, Trevor <tcr1...@gmail.com> wrote:
> hmmm.... macro question:
>
> ; here's a macro that assembles many puts. It serves no purpose to me
> (and doesn't even make sense in its current form). It's just something
> I hit playing around and learning macros.
>
> (defmacro doto-putter [x y xs]
>   `(doto (java.util.HashMap.)
>      (.put ~x ~y)
>        ~@(for [[k v] xs]`(.put ~k ~v))))
>
> user=>  (doto-putter "c" 3 {"a" 1 "b" 2})
> #<HashMap {b=2, c=3, a=1}>
>
> however:
>
> =>(defn omg [x y xs](doto-putter x y xs))
> CompilerException java.lang.IllegalArgumentException: Don't know how
> to create ISeq from: clojure.lang.Symbol, compiling:(NO_SOURCE_PATH:2)
>
> I'm thinking the issue seems to be that it's not resolving xs because
> this works:
>
> (defmacro doto-putter [x y]
>   `(doto (java.util.HashMap.)
>      (.put ~x ~y)
>        ~@(for [[k v]{"a" 1 "b" 2}]`(.put ~k ~v))))
>
> user=> (defn omg [x y](doto-putter x y))
> #'user/omg
>
> user=>(omg "c" 3)
> #<HashMap {b=2, c=3, a=1}>
>
> what am I missing? None of my other macros have this problem.
http://stackoverflow.com/questions/5813075/passing-map-of-functions-to-a-macro
is another symptom of the same problem. The point is that macros have
access only to the code they're changing, not any related/nearby
values or code, and since you gave it the *symbols* (that is, code) x,
y, and xs, it doesn't know their values. You're asking a macro to work
with runtime values, not compile-time code; it can't do that, but
fortunately a function easily can! So this is just another case of
using a macro when a function will do.

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