Re: How to get the namespace for an unqualified symbol?

106 views
Skip to first unread message

Ambrose Bonnaire-Sergeant

unread,
Nov 12, 2012, 2:04:38 PM11/12/12
to clo...@googlegroups.com
Evil, but this is the solution I use:

(let [v (resolve 'a)]
  (symbol (str (ns-name (.ns v))) (str (.sym v))))

I'd love to know a better way.

Thanks,
Ambrose

On Tue, Nov 13, 2012 at 1:46 AM, johnstok <k.w.jo...@gmail.com> wrote:
I was looking at combining the namespace and resolve functions:

user=> (def x 5)
#'user/x
user=> (resolve 'x)
#'user/x
user=> (namespace 'user/x)
"user"
user=> (namespace 'x)
nil
user=> (namespace (resolve 'x))
ClassCastException clojure.lang.Var cannot be cast to clojure.lang.Named  clojure.core/namespace (core.clj:1497)

In the final expression I was expecting to get the value "user".

Having read the doc's I understand that the return type from resolve is not compatible with namespace:

namespace: Returns the namespace String of a symbol or keyword, or nil if not present.
resolve:   Returns the var or Class to which a symbol will be resolved in the current namespace else nil.

Question: given a symbol 'x' how can I determine the 'fully qualified' symbol (i.e. 'user/x')?

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

Tassilo Horn

unread,
Nov 12, 2012, 2:14:01 PM11/12/12
to clo...@googlegroups.com
Ambrose Bonnaire-Sergeant <abonnair...@gmail.com> writes:

> Evil, but this is the solution I use:
>
> (let [v (resolve 'a)]
> (symbol (str (ns-name (.ns v))) (str (.sym v))))
>
> I'd love to know a better way.

How about backquote?

>> user=> (def x 5)
>> #'user/x

user> `x
user/x

Bye,
Tassilo

Alan Malloy

unread,
Nov 12, 2012, 2:39:06 PM11/12/12
to clo...@googlegroups.com, abonnair...@gmail.com
Yikes! Try ((juxt (comp ns-name :ns) :name) (meta (resolve 'inc))) - name information is stored on the var's meta in a clojure-friendly way - no need to use undocumented behavior on the java internals.

Stuart Sierra

unread,
Nov 12, 2012, 3:05:28 PM11/12/12
to clo...@googlegroups.com
Assuming the symbol is bound to a Var, you can do this:

(name (ns-name (:ns (meta (resolve 'x)))))

-S

Reply all
Reply to author
Forward
0 new messages