Friday, January 2, 2015

What is ^ in Clojure?

In Clojure, ^ is a map for metadata like type.

You can use it as follows:

(defn purchase-order [id date amount]
  ^{:type ::PurchaseOrder}
  {:id id :date date :amount amount})

(def my-order (purchase-order 10 (java.util.Date.) 100.0))

(println my-order)

(println (type my-order))

Then you can get the following result:

{:id 10, :date #inst "2015-01-02T12:46:44.626-00:00", :amount 100.0}
:user/PurchaseOrder

Reference:
http://clojure.org/reader

No comments:

Post a Comment