8

As a clojure beginner, I am reading clojure code to get myself familiar with Clojure's grammar. Below code snippet is a function in Ring project

(defn- request-context
"Create an UploadContext object from a request map."
{:tag UploadContext}
[request encoding]
(reify UploadContext
(getContentType [this]       (get-in request [:headers "content-type"]))
(getContentLength [this]     (or (req/content-length request) -1))
(contentLength [this]        (or (req/content-length request) -1))
(getCharacterEncoding [this] encoding)
(getInputStream [this]       (:body request))))

what is not clear to me is the line

{:tag UploadContext}

if review a clojure function's definition

(defn function-name doc-string? attr-map? [parameter-list]
conditions-map?
(expressions))

I guess (but not sure) the map should be the "attr-map?". But what is a "attr-map?"? I googled and cannot find good explanations.

any examples or links to introduce the "attr-map?" will be appreciated. I also would like to know how the attr-map is used in the code I pasted.

danny
  • 3,046
  • 3
  • 23
  • 28

1 Answers1

5

Passing an (optional) attr-map? to defn is a shorthand way of adding metadata to a function's var.

Metadata might include entries such as :tag (runtime return type), :doc (a documentation string) and :private (to indicate if the var is private to a namespace). Additional pieces of metadata can also be attached there too, but should be used for data that is "orthogonal to the logical value of the data" (quote from http://clojure.org/functional_programming)

(defn greet {:tag String :other-meta-data 5} [name] (format "Hello, %s" name))

(meta #'f)
;=> {:arglists ([name]), :ns #<Namespace user>, :name f, :end-column 8, :column 1,
     :line 4 :other-meta-data 5, :file "NO_SOURCE_FILE", :end-line 4, 
     :tag java.lang.String}
Daniel Neal
  • 4,165
  • 2
  • 20
  • 34
  • thanks Daniel. can you please share any links about the attr-map? – danny Apr 09 '14 at 01:24
  • 1
    You can read a bit about meta-data at the top of http://clojure.org/special_forms. (`attr-map?` is just a parameter name for the map of data that gets merged into the metadata on the function's var). – Daniel Neal Apr 09 '14 at 08:16
  • The value of metadata usage may differ from developer to developer. Your opinionated use of the word "should" was not in the original context of the quote "orthogonal to the logical value of the data" taken from http://clojure.org/functional_programming. It may improve the quality of your answer not to inject such bias. – ctpenrose Nov 28 '17 at 06:13