symbol literal

syntaxsince v0.0-1853 in clojure in ednEdit

A symbol is used as a name for a var. When evaluated, its result will be the value bound to the var.

  • bar - evaluates to the value bound to bar
  • foo/bar - evaluates to the value bound to a var outside the current namespace. The namespace foo can be an aliased or literal namespace.

Details:

Some naming rules:

  • must not begin with a number
  • can contain special characters . * + ! - _ ? $ % & = < > : #, as long as:
    • if starting with -, +, or ., next character cannot be numeric (would be interpreted as number)
    • cannot start with : and #
  • symbols starting or ending with a decimal are reserved for interop purposes (see . dot)

Symbols can use a single / for an optional namespace. See / namespace slash:

  • foo/bar => value of bar in the foo namespace

To access symbols in the global JavaScript context, use the js/ namespace:

  • js/document => global document JavaScript object

Dots can also be included in symbols for direct JS property access, see . dot:

  • js/console.log => the console.log JavaScript function

Examples:

The following has two symbols, def and a:

(def a 1)

The evaluation of the symbols is controlled by the evaluation of the list (def a 1). def evaluates to a special form, which suppresses the evaluation of a since it is just being used as a name for the bound value 1.

When a symbol is by itself, it will evaluated to 1:

a
;;=> 1

To signify an unevaluated symbol, precede it with a quote:

'a
;;=> a

See Also:


Reader code @ tools.reader:src/main/clojure/clojure/tools/reader.clj
(defn- read-symbol
  [rdr initch]
  (let [[line column] (starting-line-col-info rdr)]
    (when-let [token (read-token rdr :symbol initch)]
      (case token

        ;; special symbols
        "nil" nil
        "true" true
        "false" false
        "/" '/

        (or (when-let [p (parse-symbol token)]
              (with-meta (symbol (p 0) (p 1))
                (when line
                  (merge
                   (when-let [file (get-file-name rdr)]
                     {:file file})
                   (let [[end-line end-column] (ending-line-col-info rdr)]
                     {:line line
                      :column column
                      :end-line end-line
                      :end-column end-column})))))
            (err/throw-invalid rdr :symbol token))))))