In Clojure, how do I get the name of the namespace in which variables and functions are named? For example, improving the following:
(ns my-ns)
(def namespace-name "my-ns")
The problem with the above is that if I want to change the name of my-ns, I have to change the definition of namespace-name as well
A namespace is both a name context and a container for vars. Namespace names are symbols where periods are used to separate namespace parts, such as clojure. string . By convention, namespace names are typically lower-case and use - to separate words, although this is not required.
Advertisements. In Clojure, variables are defined by the 'def' keyword. It's a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable.
A simple modification of Arthur's answer works well.
(def namespace-name (ns-name *ns*))
However I want to warn Clojure beginners that
(defn namespace-name [] (ns-name *ns*))
will not work for the purposes of this question, since *ns* is bound dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With