What is the *ns* method in Clojure?

Overview

Namespaces in Clojure are used to distinguish classes into separate logical parts. We use them when we have multiple imported classes in our code. The *ns* method is one of the methods used to work with namespaces.

What is the *ns* method?

The *ns* method is used to get the current namespace of our code.

Syntax

(*ns*)
Syntax for the *ns* method

Parameter

The *ns* method does not have a parameter. That is, no parameter is passed to it.

Return value

The *ns* method returns the current namespace of your code, i.e., wherever the *ns* method is used, it returns the namespace of the code. Let's look at an example.

Example

(ns clojure.examples
(:gen-class))
(defn func []
(println *ns*))
(func)

Explanation

  • Line 3: We define a function func .
  • Line 4: We use the *ns* method and print out the output. Note that the output is an object that contains the namespace of our code.
  • Line 5: We call the function func .

Free Resources