tags:

views:

54

answers:

2

I want to check if a symbol is resolvable in the current namespace. What's the canonical way to do this?

+2  A: 

Take a look at this page. For example

(ns-map *ns*)

will give you a map of the bindings in the current namespace. You can examine this map to decide if your symbol is a key in the map,

(defn resolvable? [sym] 
  (contains? (ns-map *ns*) sym))

I do not know if this is the canonical way.

Jonas
`(contains? (ns-map *ns*) key)` does the same, probably faster.
Brian Carper
true, i'll edit that. Thanks
Jonas
+2  A: 

After sifting through the API docs once more, I've stumbled on what might be the appropriate function:

; Returns the var or Class to which the symbol
; will be resolved in the current namespace, else nil.
  (resolve 'foo)

; see also:
  (ns-resolve *a-namespace* 'foo)
Kay Sarraute
How do you tell the difference between a symbol that is unbound and a symbol that is bound to nil?
Jonas
Only the Var that is named by the symbol can be bound to nil.The symbol itself either names a Var in the current namespace, then it can be resolved, or not (then resolve returns nil).
Kay Sarraute