(dir *ns*) doesn't work??

52 views
Skip to first unread message

jaime

unread,
Oct 27, 2011, 4:57:50 AM10/27/11
to Clojure
Hi there, when I tried to execute "(dir *ns*)" in REPL, I found it
doesn't work -- with exception of:
Exception No namespace: *ns* found clojure.core/the-ns
(core.clj:3689)
I'm not sure if I used it the right way. Following are my execution
tries:
=============================================================
Clojure 1.3.0
user=> (doc dir)
-------------------------
clojure.repl/dir
([nsname])
Macro
Prints a sorted directory of public vars in a namespace
nil
user=> (dir *ns*)
Exception No namespace: *ns* found clojure.core/the-ns (core.clj:
3689)
user=> *ns*
#<Namespace user>
user=> (the-ns *ns*)
#<Namespace user>
user=> (the-ns 'user)
#<Namespace user>
user=>
=============================================================
Any suggestions??

Matjaz Gregoric

unread,
Oct 27, 2011, 5:12:09 AM10/27/11
to clo...@googlegroups.com
The dir macro is quite picky in what it accepts as arguments. Try:

(dir user)
(dir clojure.core)





--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Ben Smith-Mannschott

unread,
Oct 27, 2011, 5:12:55 AM10/27/11
to clo...@googlegroups.com
dir is a macro. It doesn't evaluate its arguments. So when you say
(dir *ns*), Clojure sees: show me what's in the namespace named
"*ns*", and there is no such namespace because *ns* is the name of a
variable which contains the name of the current namespace.

Dir is this way because for interactive use, it's convenient not to
have to quote the symbol naming the namespace. So (dir user) and (dir
clojure.core) should work.

In your situation you'll want to call dir-fn, which is a function that
will evaluate its arguments, so:

if we assume (= *ns* 'user) (i.e. you're currently in the namespace "user")
(dir-fn *ns*)
is equivalent to
(dir-fn 'user)
and
(dir user)

HTH
// Ben

jaime

unread,
Oct 27, 2011, 10:06:57 AM10/27/11
to Clojure
Oh yes you guys are right. I should have look at the source first:
(defmacro dir
"Prints a sorted directory of public vars in a namespace"
[nsname]
`(doseq [v# (dir-fn '~nsname)]
(println v#)))

from it, we can see the "quote" thing. Thank you all. I got it now!

On Oct 27, 5:12 pm, Ben Smith-Mannschott <bsmith.o...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages