Skip to content

Instantly share code, notes, and snippets.

@avescodes
Last active December 31, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avescodes/8060145 to your computer and use it in GitHub Desktop.
Save avescodes/8060145 to your computer and use it in GitHub Desktop.
Short-hands section for clojure.org/metadata

Short-hands

In addition to with-meta, there are a number of short-hand reader macros for affixing metadata to objects.

  • ^{:doc "How obj works!"} obj - Sets the metadata of obj to the provided map. Equivalent to (with-meta obj {:doc "How obj works!"}).
  • ^:dynamic obj - Sets the given keyword to true in the object's metadata. Equivalent to ^{:dynamic true} obj.
  • ^String obj - Sets the value of :tag key in the object's metadata. Equivalent to ^{:tag java.lang.String} obj. Used to hint an objects type to the Clojure compiler. See Java Interop: Type Hints for more information and a complete list of special type hints.

Each of these short-hands is a reader macro ([The Reader: Macro Characters](http://clojure.org/reader#The Reader--Macro characters)) and applies to the expression that follows it. It is possible to add multiple pieces of metadata by chaining short-hands together. For example: ^:dynamic ^ints obj would apply both the :dynamic flag and ints type-hint to obj.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment