On Jul 13, 3:58 pm, Chris Kent <cjk...@gmail.com> wrote:
> user=> (ns a) (def foo "foo")
> a=> #'a/foo
> a=>
>
> If the same code is executed inside a do form the var is defined in
> the original namespace.  I thought that the do should make no
> difference.  Is this the intended behaviour?
>
> user=> (do (ns a) (def foo "foo"))
> #'user/foo
> a=>


Hi Chris,

This involves some subtle details of how the Clojure Reader works, but
I'll try to explain what's going on.

In your first example, the Reader reads the (ns...) form first, then
evaluates it, which sets the current namespace to "a".  Then the
Reader reads the (def...) form, while it is in namespace "a".  So, to
emphasize, we are in namespace "a" when the (def...) form is READ.

In your second example, the Reader reads the entire (do...) form
before anything gets evaluated.  At that time, we are still in the
"user" namespace.  So the (def...) form gets READ in namespace "user".

The crux is this: (def...) is a special form, so the usual rules don't
apply.  In this case, what matters is when the (def...) form gets
READ, not when it gets evaluated.  To get the behavior you expect, try
the "intern" function.

-Stuart Sierra
--~--~---------~--~----~------------~-------~--~----~
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