Clojure - Namespaces

1 minute read

Namespaces

Namespaces contain maps between between human-friendly symbols and references known as vars. Clojure allows for chaning of namespaces and creating new ones.

To create a namespace we can use the function create-ns, the function in-ns and the macro ns. create-ns takes a symbol and creates a namespace, in-ns does that and moves into the created
namespace.

refer

refer allows us to bring stuff from other namespaces into the current one. If called on its own it brings everything from the namespace, but it can also be called with the filters :only, exclude and rename. We can also define private functions which can’t be brought to other namespaces using defn-

1(in-ns 'space.private)
2(defn- private-function
3  [])
4
5(in-ns 'space.other)
6(space.private/private-function) 
7(refer 'space.private :only ['private-function])

The last two lines would throw an error.

alias

alias allows us to shorten a namespace name for using fully qualified symbols.

1(clojure.core/alias 'new 'space.new)
2(new/some-fn)

ns macro

The ns macro is used when working with namespaces in files because it provides many functionalities. You can use references with ns to use different functions of the macro. The six possible references are:

  • (:refer-clojure)
  • (:require)
  • (:use)
  • (:import)
  • (:load)
  • (:gen-class)

(:require) works like the require function just with different syntax. You can require any number of files and use :as to alias them to different names. :refer can be used in the same way as the refer function.

1(ns exmaple.core
2  (:require [clojure.string :as string]
3            [clojure.set :refer :all]))