Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: referring to the name of the namespace

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

like image 270
chris Avatar asked Sep 21 '10 19:09

chris


People also ask

What is Clojure namespace?

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.

What is a VAR in Clojure?

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.


1 Answers

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.

like image 132
chris Avatar answered Oct 09 '22 00:10

chris