liveBook · Manning

Syntax-unquote

As explained in Clojure: The Essential Reference

Unquote is syntax inside a syntax-quote expression that evaluates an embedded expression and inserts its result.

Unquote (`~`) evaluates an expression inside syntax-quote and inserts its result into the resulting form. It makes syntax-quote useful for building macro expansions from a template. section 4.1

`[1 2 (+ 1 2)  ~(+ 1 2)]
;; [1 2 (clojure.core/+ 1 2) 3] ; #1

Compare the two additions: the one without `~` remains a form, while `~(+ 1 2)` becomes `3`. section 4.1

One value or several elements

`~[3 4]` inserts the vector as one value. In a surrounding syntax-quoted collection, `~@[3 (+ 1 2)]` inserts its elements separately; the result is `[1 2 [3 4] 3 3]`. section 4.1

Unquoting a quoted symbol with `~'foo` leaves it unqualified. Avoid that pattern unless the unqualified symbol is intentional: it bypasses syntax-quote’s protection against accidental symbol capture. section 4.1

How Clojure: The Essential Reference covers it

How deep it goes
Defines the insertion behavior and demonstrates it with results, without developing reader or compiler internals.
Examples
Compares quoted and unquoted addition, vector insertion and splicing, and an unqualified-symbol pattern.
What it assumes
Assumes familiarity with Clojure expressions while introducing syntax-quote and its unquote notation.
What you can do after
Apply it via Clojure syntax-quote to insert evaluated values into generated forms while distinguishing insertion from splicing.
How it connects
Connects unquote to syntax-quote, macros, unquote-splicing, symbol qualification, and accidental symbol capture.

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.