liveBook · Manning

Library

As explained in Clojure: The Essential Reference

A Clojure library is a source file whose name corresponds to the namespace declaration it contains and which follows conventions for storing and reusing Clojure code.

A Clojure library is a source file used to store and reuse code. It declares a namespace whose name reflects the file’s relative path: path separators become dots. The file must be available on the classpath for `require` to find it. section 16.0 section 17.3

A library and a namespace are closely related, but they are not interchangeable. Loading a library creates its namespace; creating a namespace alone does not create a file that can be loaded. `require` loads a library and its transitive dependencies, while `loaded-libs` reports libraries already loaded. section 16.0 section 16.10

Load a library

`(require 'clojure.set)` makes the `clojure.set` namespace available. Use an alias, such as `:as se`, when you want access without referring every symbol into the current namespace; `:refer :all` can replace existing mappings and produce warnings. section 16.10

(create-ns 'test-require) ; #1
(require 'test-require) ; #2
;; Could not locate test_require__init.class or test_require.clj on classpath.

Creating `test-require` as a namespace does not make it a library: `require` still fails because it cannot locate a corresponding file on the classpath. section 16.10

For direct evaluation, `load` reads from a classpath-relative path, while `load-file` can evaluate a file outside the classpath. `load-string` evaluates code supplied as a string and, unlike `read-string` followed by `eval`, handles multiple forms without a `do` wrapper and retains line numbers in var metadata. section 17.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines file and namespace conventions, then distinguishes library loading from namespace creation and direct file evaluation.
Examples
Uses `clojure.set` loading, a failed `require` of a fileless namespace, and warnings from referring all symbols.
What it assumes
Defines libraries from source files and namespaces while assuming familiarity with Clojure forms and the classpath.
What you can do after
Operate/configure it: load file-backed namespaces with `require`, inspect `loaded-libs`, and choose aliases over broad symbol imports.
How it connects
Connects libraries to namespaces, classpaths, transitive dependencies, aliases, and evaluation.

Also explained in

Compare how 2 books explain library →

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.