UUID

typesince v0.0-1424Edit
satisfies IComparable IEquiv IHash IPrintWithWriter IUUID

(UUID. uuid __hash)

Details:

A type representing a universally unique identifier (UUID).

Use uuid or #uuid literal to create one.


Examples:

;; Can be used to generate UUIDs but cljs.core/uuid is preferred
(UUID. (str (random-uuid)))
=> #uuid "d9b3d2a1-3f2a-4662-ae48-bfd7fe39e2a6"

;; UUID allows manual setting of hash values, which can make two otherwise
;; identical UUIDs no longer equal
(def nil-uuid #uuid "00000000-0000-0000-0000-000000000000")
(def custom-hash-nil-uuid (UUID. #uuid "00000000-0000-0000-0000-000000000000" 13))

(= nil-uuid #uuid "00000000-0000-0000-0000-000000000000")
=> true

(= custom-hash-nil-uuid #uuid "00000000-0000-0000-0000-000000000000")
=> false

(hash custom-hash-nil-uuid)
=> 13

(hash nil-uuid)
=> 1297391103


See Also:


Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(deftype UUID [uuid ^:mutable __hash]
  IUUID

  Object
  (toString [_] uuid)
  (equiv [this other]
    (-equiv this other))

  IEquiv
  (-equiv [_ other]
    (and (implements? IUUID other) (identical? uuid (.-uuid other))))

  IPrintWithWriter
  (-pr-writer [_ writer _]
    (-write writer (str_ "#uuid \"" uuid "\"")))

  IHash
  (-hash [this]
    (when (nil? __hash)
      (set! __hash (hash uuid)))
    __hash)

  IComparable
  (-compare [this other]
    (if (instance? UUID other)
      (garray/defaultCompare uuid (.-uuid other))
      (throw (js/Error. (str_ "Cannot compare " this " to " other))))))