syntax | since v0.0-1853 | in clojure | in edn | Edit |
Some frequently used value types are afforded a "tagged literal" syntax. It is similar to a constructor, but this special syntax makes it de/serializable and easier to read at the REPL.
Tagged literals start with a #
followed by a symbol and a literal:
#js [...]
- JavaScript array literal#js {...}
- JavaScript object literal#inst "..."
- JavaScript date literal#uuid "..."
- UUID literal#queue [...]
- queue literalThese tagged literals are the "extensible" part of extensible data notation (edn), with ClojureScript being a superset of edn.
(Tagged literals are printed as themselves, just like core syntax literals.)
literal JavaScript object:
#js {:foo 1}
;;=> #js {:foo 1}
(def foo 1)
#js {:foo foo}
;;=> #js {:foo 1}
literal queue:
#queue [1 2 3]
;;=> #queue [1 2 3]
(conj #queue [1 2 3] 4)
;;=> #queue [1 2 3 4]
literal instant of time (date):
#inst "2014-10-13"
;;=> #inst "2014-10-13T00:00:00.000-00:00"
literal UUID:
#uuid "97bda55b-6175-4c39-9e04-7c0205c709dc"
;;=> #uuid "97bda55b-6175-4c39-9e04-7c0205c709dc"
(defn- read-tagged [rdr initch opts pending-forms]
(let [tag (read* rdr true nil opts pending-forms)]
(if-not (symbol? tag)
(err/throw-bad-reader-tag rdr tag))
(if *suppress-read*
(tagged-literal tag (read* rdr true nil opts pending-forms))
(if-let [f (or (*data-readers* tag)
(default-data-readers tag))]
(f (read* rdr true nil opts pending-forms))
(if (.contains (name tag) ".")
(read-ctor rdr tag opts pending-forms)
(if-let [f *default-data-reader-fn*]
(f tag (read* rdr true nil opts pending-forms))
(err/throw-unknown-reader-tag rdr tag)))))))