Search code examples
syntaxclojureclojurescriptom

Clojure — Meaning of pound symbol?


In the Om Next Quick Start, they use #js and #uuid. What does the pound symbol here mean?

Link: https://github.com/omcljs/om/wiki/Quick-Start-(om.next)#components-with-queries--mutations

Snippets:

#js:

(defui Counter
  static om/IQuery
  (query [this]
    [:count])
  Object
  (render [this]
    (let [{:keys [count]} (om/props this)]
      (dom/div nil
        (dom/span nil (str "Count: " count))
        (dom/button
          #js {:onClick
               (fn [e] (om/transact! this '[(increment)]))}
          "Click me!")))))

#uuid:

(om/from-history reconciler
  #uuid "9e7160a0-89cc-4482-aba1-7b894a1c54b4")

Solution

  • Commonly found in EDN and ClojureScript this use of # is called the tagged literal. Look at this example,

    user=> (java.util.Date.)
    #inst "2014-05-19T19:12:37.925-00:00"
    

    When we create a new date it is represented as a tagged literal, or in this case a tagged string. We can use Clojures read-string to read this back (or use it directly)

    user=> (type #inst "2014-05-19T19:12:37.925-00:00")
    java.util.Date
    (read-string "#inst \"2014-05-19T19:12:37.925-00:00\"")
    #inst "2014-05-19T19:12:37.925-00:00"
    user=> (type (read-string "#inst \"2014-05-19T19:12:37.925-00:00\""))
    java.util.Date
    

    A tagged literal tells the reader how to parse the literal value. Other common uses include #uuid for generating UUIDs and in the ClojureScript world an extremely common use of tagged literals is #js which can be used to convert ClojureScript data structures into JavaScript structures directly.

    Courtesy: The Weird and Wonderful Characters of Clojure