How to check if a symbol is resolvable in the current namespace? - clojure

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

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)

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.

Related

How to use a symbol only if it is defined

I would like to execute some Clojure code that depends upon a certain var, but only if that var is defined. As a simplified example, the body of the if form should only be executed if sym is defined:
(if (resolve 'sym) (println sym))
Unfortunately, this doesn't work. If sym is not defined, the compiler still tries to resolve it and throws:
CompilerException java.lang.RuntimeException: Unable to resolve symbol: sym in this context
From reading Rich Hickley's comment here, I gathered that this behavior is due to Clojure's use of (mostly) single-pass compilation. However, as much sense as that makes, it obviously results in undesirable behavior in this case.
I can get around the problem by forcing the symbol resolution to happen at runtime:
(if (resolve 'sym) (println (deref (resolve 'sym))))
But this is an undesirable hack. Is there a better way, or is it not possible with Clojure's read-eval model?
(Why do I need to do this? I have multiple composable profiles defined in my profiles.clj. One of them is for vinyasa, which allows me to inject various functions into the conveniently accessible . namespace. Others load various other utility libraries. In the profiles for those other utility libraries, I want to use vinyasa to inject the most important functions, but only if vinyasa is loaded. I am currently using a variation of the hack above.)
The approach recommended by #Michiel with when-let is the best way to solve this problem. Importantly, you can make the conditionality almost completely transparent by using let's ability to shadow an existing binding:
(when-let [foo (resolve 'foo)]
(foo))
;;=> nil
(defn foo []
:bar)
(foo)
;;=> :bar
(when-let [foo (resolve 'foo)]
(foo))
;;=> :bar
(defn get-when-var-defined [sym]
(when-let [var-for-sym (resolve sym)]
#var-for-sym))
(get-when-var-defined 'foo) ;;=> nil
(def foo 1)
(get-when-var-defined 'foo) ;;=> 1

How can I prevent symbol contamination between Clojure tests?

I use simulation-style tests in to ensure that my entire application works correctly. The core Clojure test library is used for tests, executed via Leiningen. As the -main function runs, it defines symbols for later use within its logic. The problem is that if I accidentally use a symbol created in one -main test but never defined in the current -main test, it still has a value. I would expect to get an error that the symbol is undefined, but it seems my test environment is somehow sharing state between deftest executions. How can I deal with this? Move all my convenience-driven symbol definitions to a let statement?
If you are def-ing global vars inside your function, that's generally considered bad practice and reason enough to use let as you suggest instead.
However, you can capture a snapshot the mappings of your namespace.
(def ns-snapshot (ns-map *ns*))
So that after you intern symbols
(def foo 1)
(def bar 2)
You can determine the additions
(reduce dissoc (ns-map *ns*) (keys ns-snapshot))
;=> {bar #'so.core/bar, foo #'so.core/foo}
And un-map them
(doseq [[k v] (reduce dissoc (ns-map *ns*) (keys ns-snapshot))] (ns-unmap *ns* k))
So that you'll get the desired undefined error again
foo ;=> CompilerException ... Unable to resolve symbol: foo in this context

How to find the fully qualified namespace of a symbol?

If I have a symbol who's namespace is an alias, like q/w, how can I find its actual namespace, say actual.namespace/w ?
I know that resolve will give me the fully qualified var, but I don't know how to get the namespace of a var.
The best I can do is:
(defn fqns [s] (str (get (ns-aliases *ns*) (symbol (namespace s)))))
surely there's a simpler way ?
You can get the namespace object of a symbol as shown below (if you want name of ns as string then just call str at the end):
(defn fqns [s] (->> (resolve s) meta :ns))
The symbol itself has no direct connection to any namespace object; its namespace part is just an interned string. It is only when it's being resolved that this string is treated as the name of an actual namespace in which to look up a Var. (In particular, you can create a symbol foo/bar without creating a namespace foo or registering foo as an alias for a namespace.) Thus, you need to either go through resolve or do part of its work yourself.
In the first case, you can say (in recent versions of Clojure; or you can skip the hyphens signifying property access -- instead of -ns, use ns -- to be backwards compatible)
(.. (resolve s) -ns -name)
This resolves a Var object, then extracts a reference to its namespace from its ns field and finally extracts the symbolic name of the namespace from its name field. (Note that the namespace and name functions wouldn't work, since Vars and namespaces do not implement clojure.lang.Named.)
The function from the question text is fine too, although it'll be better to extract the symbol name from the namespace -- (.-name (get ...)) -- rather than rely on the toString representation of namespaces through the use of str.
You may also want to add clojure.lang.Var and clojure.lang.Namespace type hints to avoid reflection in your calls. (If you want them to be faster, or if you need *warn-on-reflection* to tweak something else for performance and wish to avoid useless warnings here.)
If you wish to handle the possibility that the namespace does not exist (and return nil in that case, in keeping with usual Clojure idiom):
(defn fqns
([s]
(fqns (.-name *ns*) s))
([ns s]
(some-> ns
find-ns
.-name
ns-aliases
(get (symbol (namespace s)))
.-name)))
Or use some if-lets in Clojure 1.4 and earlier.

Clojure : Determine if a variable is declared

How can I test wether a variable has been declared or assigned (i.e. check if "a" is defined, when I expect a program to call some code like this (def a (create-a)) ?
And related --- how does the answer to this question relate to the problem of resolving a symbol (i.e. a function) which has been declared ? Clojure: determine if a function exists
It seems like a defined variable should be checkable in the same sense that a defined function is, but I'm finding that the solution for determining if a function exists is not sufficient for determining wether a variable exists.
Some context : I'm writing unit tests for a multideveloper project, and want to make sure that the test data, and the methods in different classes have been defined. Since there is no good IDE support for clojure, it seems to me that, given its loose structure, it is good to test method names and variable names existence before testing their outputs / content.
You can use resolve to see if the variable was bound/defined:
(resolve 'meaning)
nil
(def meaning 42)
#'user/meaning
(resolve 'meaning)
#'user/meaning
or you can boolean check it, if you need true/false:
(boolean (resolve 'meaning))
true
One way to do this is to use ns-resolve, for example:
user=> (def a "hello a")
user=> (ns-resolve *ns* 'a)
#'user/a
user=> (ns-resolve *ns* 'b)
;nil ; This assumes b hasn't been defined before...
Note that if you namespace-qualify the symbol to be checked then what you pass as first argument (*ns* in the example above) doesn't matter:
user=> (ns-resolve 'user 'a)
#'user/a
user=> (ns-resolve 'other 'a)
nil
user=> (ns-resolve 'other 'user/a)
#'user/a
The resolve function mentioned by #tolitius is actually a shorthand for ns-resolve where the namespace argument always evaluates to ns, depending on the use case it might be more handy.
As others have said, resolve will return the var for a symbol if there is one defined, or nil. Further, you can check if the var has a value bound to it by using bound?.
user=> (resolve 'foo)
nil
user=> (def foo)
#'user/foo
user=> (resolve 'foo)
#'user/foo
user=> (bound? #'foo)
false
user=> (def foo 5)
#'user/foo
user=> (bound? #'foo)
true
Since there is no good IDE support for clojure it seems to me that,
given its loose structure, it is good to test method names and
variable names existence before testing their outputs / content.
This is nuts. You really want a test to say "Oops! You forgot to define foobar!" instead of just trying to run foobar and seeing Clojure's "Unable to resolve symbol" message?
What do you gain from this? You lose the stacktrace, which could be useful if, for example, the test is passed the wrong function name by some other code. Much better to know what line mis-spelled foobar than to search through your whole test namespace.

Avoid overriding variable names

On a particular namespace I am working on I am beginning to run out of function names. Is there a way to get a warning like the one I get if I override a symbol from another namespace if I reuse a symbol which is already bound to a function in the same namespace?
If this is enough of a problem that you'd be willing to replace a (set of) core macro(s), you could try this approach:
(ns huge.core
(:refer-clojure :exclude [defn]))
(defmacro defn [name & defn-tail]
(assert (nil? (resolve name))
(str "Attempting to redefine already defined Var "
"#'" (.name *ns*) "/" name))
`(clojure.core/defn ~name ~#defn-tail))
Then any attempt to redefine an existing Var with defn will fail:
user=> (defn foo [] :foo)
#'user/foo
user=> (defn foo [] :bar)
AssertionError Assert failed: Attempting to redefine already defined Var #'user/foo
(nil? (resolve name)) user/defn (NO_SOURCE_FILE:2)
You could similarly replace defmacro; in that case you would have to call clojure.core/defmacro when defining your own variant.
Plain, unadorned def is a special form and receives magic treatment from the compiler, so you could still overwrite existing Vars with it. If you would like to guard against name clashes on that flank too, you could switch to something like defvar (used to be available in clojure.contrib.def) with a similar custom assert.
This isn't quite an answer to your question but may help avoid the issue depending on how the functions in your namespace are being used. You could make them into local functions using letfn, allowing you to reuse names for functions that are only used within the context of another function.
(defn main-fn [x]
(letfn [(secondary-fn [x] (* x x))
(another-fn [x] (secondary-fn (inc x)))]
(/ (another-fn x) 4)))
Even if you restrict yourself to single-character function names, you are in no danger of running out, as there are (about) 64 thousand Unicode characters, any one of which is a valid function name.
Given that you can in fact have names that are ten thousand characters long, you are on even safer ground.