= Namespaces
Rich Hickey
2015-01-01
:type: reference
:toc: macro
:icons: font
:prevpagehref: metadata
:prevpagetitle: Metadata
:nextpagehref: libs
:nextpagetitle: Libs

ifdef::env-github,env-browser[:outfilesuffix: .adoc]

toc::[]

Namespaces are mappings from simple (unqualified) symbols to Vars and/or Classes. Vars can be interned in a namespace, using _**def**_ or any of its variants, in which case they have a simple symbol for a name and a reference to their containing namespace, and the namespace maps that symbol to the same var. A namespace can also contain mappings from symbols to vars interned in other namespaces by using https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/refer[refer] or https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/use[use], or from symbols to Class objects by using https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/import[import]. Note that namespaces are first-class, they can be enumerated etc. Namespaces are also dynamic, they can be created, removed and modified at runtime, at the Repl etc.

The best way to set up a new namespace at the top of a Clojure source file is to use the https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns[ns macro]. By default this will create a new namespace that contains mappings for the classnames in +java.lang+ plus `clojure.lang.Compiler`, and the functions in `clojure.core`.

At the Repl it's best to use https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/in-ns[in-ns], in which case the new namespace will contain mappings only for the classnames in +java.lang+. In order to access the names from the `clojure.core` namespace you must execute +(clojure.core/refer 'clojure.core)+. The `user` namespace at the Repl has already done this.

The current namespace, _pass:[*ns*]_ can and should be set only with a call to https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/in-ns[in-ns] or the https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns[ns macro], both of which create the namespace if it doesn't exist.

== Related functions

[%hardbreaks]
Creating and switching to a namespace: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/in-ns[in-ns] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns[ns] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/create-ns[create-ns]
Adding to a namespace: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/alias[alias] <<special_forms#def,def>> https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/import[import] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/intern[intern] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/refer[refer]
Finding what namespaces exist: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/all-ns[all-ns] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/find-ns[find-ns]
Examining a namespace: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-name[ns-name] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-aliases[ns-aliases] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-imports[ns-imports] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-interns[ns-interns] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-map[ns-map] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-publics[ns-publics] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-refers[ns-refers]
Getting a namespace from a symbol: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/resolve[resolve] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-resolve[ns-resolve] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/namespace[namespace]
Removing things: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-unalias[ns-unalias] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/ns-unmap[ns-unmap] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/remove-ns[remove-ns]

== More information

Also see:

* <<xref/../../../guides/learn/namespaces#,Learn Clojure - Namespaces>>
* <<vars#,Reference - vars>>
