liveBook · Manning

Docstring

As explained in Clojure: The Essential Reference

A docstring is a string associated with a function definition that describes the function for users and tools.

A docstring is an optional string placed after a function’s name in `defn`. It describes the function and is stored in the resulting Var’s metadata, where documentation tools can find it. section 2.1.1

(defn hello
  "A function to say hello"    ; #1
  [person]
  (str "Hello " person))

(clojure.repl/doc hello)       ; #2
;; -------------------------
;; user/hello
;; ([person])
;;   A function to say hello

;; ([person])
;;     A function to say hello ; #3
;; nil

(:doc (meta #'hello))          ; #4

;; "A function to say hello"

The `hello` definition shows the string in place. `clojure.repl/doc` displays it alongside the function’s arity, while `(:doc (meta #'hello))` retrieves it directly. section 2.1.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines its optional placement and Var storage, then demonstrates two ways to inspect it.
Examples
Shows a greeting function’s docstring displayed by `clojure.repl/doc` and retrieved from metadata.
What you can do after
Apply it via Clojure `defn` and `clojure.repl/doc` to attach and inspect function documentation.
How it connects
Connects function definitions, Var metadata, and REPL documentation lookup.

Also explained in

Compare how 6 books explain docstring →

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.