syntax | since v0.0-1853 | in clojure | in edn | Edit |
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.Some naming rules:
. * + ! - _ ? $ % & = < > : #
, as long as:
-
, +
, or .
, next character cannot be numeric (would be interpreted as number):
and #
. dot
)Symbols can use a single /
for an optional namespace. See / namespace slash
:
foo/bar
=> value of bar
in the foo
namespaceTo access symbols in the global JavaScript context, use the js/ namespace
:
js/document
=> global document
JavaScript objectDots can also be included in symbols for direct JS property access, see . dot
:
js/console.log
=> the console.log
JavaScript functionThe 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
(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))))))