Namespaces

Namespaces are organized and controlled access to vars. Creation and switching to new nampspace can be done with ns.

(ns foo.bar)

Current namespace can be inspected as follows.

*ns*
;; -> #<Namespace foo.bar>

Var definitions can be directly accessible or with fully qualified namespace.

(def hello "world") 
;; -> #'foo.bar/hello

hello
;; -> "world"

foo.bar/hello
;; -> "world"

Switching to another namespace, the symbol will no longer be resolved. Fully qualified namespace resolves the symbol.

(ns diff.namespace) 
;; -> nil

hello
;; -> CompilerException java.lang.RuntimeException:
;; -> Unable to resolve symbol: hello in this contex

foo.bar/hello
;; -> "world"

If load a namespace, require is used.

(require 'clojure.set)

Another way to do the above is to alias it.

(ns lalaland)
;; -> nil

(require '[foo.bar :as fb])
;; -> nil

fb/hello
;; -> "world"

It is common to see nested in ns.

(ns lalaland
    (:require [foo.bar :as fb]))

fb/hello
;; -> "world"