liveBook · Manning

Literal

As explained in Clojure: The Essential Reference

A compile-time literal is a fixed Clojure value recognized by the compiler without evaluating an expression to obtain it.

A compile-time literal is a fixed value that `case` can use as a test without evaluating a test expression. `case` evaluates its input, selects a matching branch, and evaluates that branch’s result. Its constant lookup is intended for faster branching than a linear `cond` test. section 3.3.4

A literal test

`(case 1 0 "zero" 1 "one" :other)` returns `"one"`. The test `1` is a literal; the result form is evaluated only when its test matches. section 3.3.4

Tests can include keywords, numbers, strings, characters, ratios, regexes, and collection literals. Do not use `(inc 0)` expecting it to become the test `1`: `case` does not evaluate that form as an expression. Parenthesized tests instead group alternatives for inclusion. Repeated test constants are an error; without a match, `case` returns its default or throws if none is supplied. section 3.3.4

How Clojure: The Essential Reference covers it

How deep it goes
Defines accepted test forms and explains why expression-shaped tests are not evaluated, including duplicate-test and missing-default errors.
Examples
Uses numeric branches, expression-shaped tests, quoted-symbol collisions, and grouped operator names.
What it assumes
Assumes familiarity with Clojure forms and introduces the literal-test restriction through examples.
What you can do after
Apply it via `case` to choose valid constant tests and avoid mistaking expressions for evaluated tests.
How it connects
Connects compile-time literals to `case`, `cond`, macro expansion, and JVM tableswitch lookup.

Also explained in

Compare how 6 books explain literal →

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.