#uuid literal

tagged literalsince v0.0-1424 in clojure in ednEdit

A universally unique identifier (UUID). Randomly generate one with random-uuid.

  • #uuid "8-4-4-4-12" - numbers represent the number of hex digits
  • #uuid "97bda55b-6175-4c39-9e04-7c0205c709dc" - actual example

Details:

Uses the UUID type.

Representing UUIDs with #uuid rather than just a plain string has the following benefits:

  • the reader will throw an exception on malformed UUIDs
  • its UUID type is preserved and shown when serialized to edn.

To create a UUID from an evaluated expression, use uuid.


Examples:

#uuid "00000000-0000-0000-0000-000000000000"
;;=> #uuid "00000000-0000-0000-0000-000000000000"

#uuid "97bda55b-6175-4c39-9e04-7c0205c709dc"
;;=> #uuid "97bda55b-6175-4c39-9e04-7c0205c709dc"

#uuid "asdf"
;; clojure.lang.ExceptionInfo: Invalid UUID string: asdf

Get as a string:

(def foo #uuid "97bda55b-6175-4c39-9e04-7c0205c709dc")
(str foo)
;;=> "97bda55b-6175-4c39-9e04-7c0205c709dc"

See Also:


Reader code @ clojurescript:src/main/clojure/cljs/tagged_literals.cljc
   (defn read-uuid
     [form]
     (when-not (string? form)
       (throw (RuntimeException. "UUID literal expects a string as its representation.")))
     (try
       (java.util.UUID/fromString form)
       (catch Throwable e
         (throw (RuntimeException. (.getMessage e))))))

Reader table @ clojurescript:src/main/clojure/cljs/tagged_literals.cljc
(def ^:dynamic *cljs-data-readers*
  (merge ;; assumes we can read all data_readers
    #?(:clj *data-readers*)
    {'queue read-queue
     'uuid  read-uuid
     'inst  read-inst
     'js    read-js}))