eval does work at macro expansion time, but only where it is actually
possible to evaluate the expression its given with only the
information available at macro expansion time at hand.

One case where there is not enough information -- and a minimal "Can't
eval locals" example -- is the following:

(defmacro foo [x] (eval x))

(let [y 1] (foo y))

Are you doing something similar...?

eval's behaviour here is very sensible, since it is asked to eval the
Symbol y at compile time, when it knows that it names a local binding,
but does not know what the value of that local binding is going to be
at runtime. (It could conceivably notice that this particular local is
given a constant initializer expression, but that would only raise
expectations for cases like (let [y (some-function)] (foo y)) where
using the compile-time return value of (some-function) would make no
sense even if it were possible to obtain it.)

That doesn't mean eval'ing stuff at compile time is not possible, it's
just that one has to make sure that one is eval'ing forms which have
meaningful compile-time values.

Sincerely,
Michał


PS. Actually it's fun to go code diving to see how this breaks
exactly... Although it's not terribly helpful in writing code. A
summary of the code path this goes through is, I think, that
clojure.core/eval delegates to clojure.lang.Compiler.eval, which in
turn needs the expression analysed by clojure.lang.Compiler.analyze,
which in this case delegates to clojure.lang.Compiler.analyzeSymbol,
which in this case -- called at compile-time inside this particular
let form -- finds a binding for the Symbol y included in the local
environment and accordingly returns a LocalBindingExpr, which is
something un-eval-able.

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