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
2009-09-11 18:53:45
`(contains? (ns-map *ns*) key)` does the same, probably faster.
Brian Carper
2009-09-11 19:31:21
true, i'll edit that. Thanks
Jonas
2009-09-11 19:34:00
+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
2009-09-11 19:25:01
How do you tell the difference between a symbol that is unbound and a symbol that is bound to nil?
Jonas
2009-09-11 19:31:15
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
2009-09-11 23:28:36