*ns*

added
1.0

ns
clojure.core

type
var

A clojure.lang.Namespace object representing the current namespace.

                user=> *ns*
#<Namespace user>
            
                user=> (ns foo.bar)
nil

foo.bar=> *ns*
#<Namespace foo.bar>
            
                ;; A (rare) trap which can happen is attempting to dynamically call `in-ns`, due to how
;; `*ns*` is really an instance of `clojure.lang.Var` with a root binding.
user=> (ns my.namespace)
nil

my.namespace=> *ns*
#<Namespace my.namespace>

;; This will *only* work in the REPL (or REPL-like environments)!!
;; Tools like Boot and Lein may or may not provide this (read the manual/code)
my.namespace=> (defn swap-ns! [ns-name] (in-ns ns-name))
#'my.namespace/swap-ns!

my.namespace=> (swap-ns! 'other.ns)
#namespace[other.ns]

;; Later, at runtime...
;; Throws IllegalStateException("Can't change/establish root binding of: *ns* with set")
;; Remember, *ns* is a root var and in-ns calls set!, which only works after
;; someone somewhere calls the binding macro (or Java equivalent)
(defn -main
  [& args]
  (println *ns*)
  (swap-ns! 'arbitrary-namespace))
;; prints #namespace[clojure.core] and then will crash