On Thu, Feb 10, 2011 at 12:06 AM, Andreas Kostler
<andreas.koestler.le...@gmail.com> wrote:
> Hi Ken,
> Thanks again. However, I still have one nagging gap in understanding this 
> macro.
> If I replace
>
> (defmacro time-limited [ms & body]
>  `(let [f# (future ~@body)]
>    (. get f# ~ms java.util.concurrent.TimeUnit/MILLISECONDS)))
>
>
> which expands to:
> (macroexpand-1 '(time-limited 1000 (println "Hello")))
> (clojure.core/let [f__1760__auto__ (clojure.core/future (println "Hello"))] 
> (.get f__1760__auto__ 1000 java.util.concurrent.TimeUnit/MILLISECONDS))
>
> with:
> (defmacro time-limited-2 [ms & body]
>  `(let [f (future ~@body)]
>    (. get f ~ms java.util.concurrent.TimeUnit/MILLISECONDS)))
>
> which expands to:
> (macroexpand-1 '(time-limited-2 1000 (println "Hello")))
> (clojure.core/let [user/f (clojure.core/future (println "Hello"))] (.get 
> user/f 1000 java.util.concurrent.TimeUnit/MILLISECONDS))
>
> So far so good. Now time-limited-2 bombs out with the following exception:
> Can't let qualified name: user/f
>  [Thrown class java.lang.Exception]
>
> Why is that?

Protection against accidentally writing unhygienic macros.
Syntax-quoted names without the # on them become namespace-qualified.
This has a couple of useful effects.

One of those is that your macro above fails cleanly rather than
working much of the time but then acting up if the user uses the name
f around it.

Also, if your macro, say, emits code that adds two numbers with +, if
someone later does

   (ns foo
     (refer-clojure (:except +))
     (refer [foo/bar/baz :as baz]
     ...)

   (defn + [v1 v2]
     ; My vector-adding +
     (map clojure.core/+ v1 v2))

   (defn foo [x y z]
     ...
     (baz/plus-using-macro something mumble x y z
       (body-stuff))
     ...)

plus-using-macro doesn't exhibit strange failures from the oddball
local definition of +, instead using clojure.core/+ always.

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