On Jul 27, 11:56 am, Dmitry Gutov <raa...@gmail.com> wrote:
> > First: Why doesn't macroexpand expand the inner when-lets?
>
> It's not supposed to, see the doc. To do full expansion, you can use
> `clojure.walk/macroexpand-all`.
>
> > Is the gensym in the two expands the same thing, or do "they" get the
> > same name? That was surprising to me. I can't think of any real
> > example where that is a problem. But what if I had wanted to write a
> > macro like the second when-lets?
>
> Looks like a bug/limitation of the gensym reader macro, these symbols
> are treated as belonging to the same quoted form, so they get the same
> name.

Nothing to do with being the same quoted form: foo# symbols are
gensymmed at read time, so by the time the macro runs there's no
evidence that they were ever gensyms: just a symbol named
temp__1551__auto__, and it has no reason to suppose you intend them to
be different.

I don't think this is a problem for many (any?) macros, but if you
want more control you can gensym yourself:

(defmacro when-lets
  [bindings & body]
  (if-not (seq bindings)
    `(do ~@body)
    (let [temp (gensym) form (bindings 0) tst (bindings 1) rst (drop 2
bindings)]
      `(let [~temp ~tst]
         (when ~temp
           (when-lets ~(vec rst)
             (let [~form ~temp]
               ~@body)))))))

user> (use 'clojure.walk) (macroexpand-all '(when-lets [a 1 b 2] [a
b]))
(let* [G__1815 1] (if G__1815 (do (let* [G__1816 2] (if G__1816 (do
(do (let* [b G__1816] (let* [a G__1815] [a b])))))))))

user> (when-lets [a 1 b 2] [a b])
[1 2]

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