liveBook · Manning

Namespace mapping table

As explained in Clojure: The Essential Reference

A namespace mapping is the association between names and vars maintained within a Clojure namespace.

A namespace mapping table is the dictionary associated with a Clojure namespace. It maps symbols to objects, especially vars and classes, and provides the main mechanism for resolving names during evaluation. section 16.0 section 16.8

The mapping table changes as namespaces are created and configured:

  • Creating a namespace adds default imports; ns also adds public clojure.core vars, making standard functions directly available. section 16.6
  • ns-map returns the table, whose entries commonly have symbol keys and var or class values. section 16.8
  • ns-unmap removes a symbol from one namespace without deleting the underlying function or var from the system; refer can restore access. section 16.8

Inspecting and using a mapping

A mapping can be treated as a map lookup: (def user-ns-mapping (ns-map 'user)) obtains the table, and ('+ user-ns-mapping) retrieves the var for +. Calling that result with 1 and 1 produces 2. section 16.0

(def user-ns-mapping (ns-map 'user))  ; #1
(def lookup-var ('+ user-ns-mapping)) ; #2
(lookup-var 1 1) ; #3
;; 2

Browse the compact example showing namespace inspection, symbol lookup, var retrieval, and invocation. section 16.0

How Clojure: The Essential Reference covers it

How deep it goes
Defines the table, describes symbol-keyed entries and mutations, and demonstrates ns-map lookup without descending into evaluator internals.
Examples
Uses user and myns mappings, symbol lookup, removal of +, and restoration through refer.
What it assumes
Assumes familiarity with Clojure symbols and namespaces while defining how vars, classes, and namespace creation participate in lookup.
What you can do after
Diagnose name availability by inspecting ns-map and modifying entries with ns-unmap or refer.
How it connects
Connects namespaces, vars, classes, ns-map, ns-unmap, refer, and namespace creation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.