On Thu  8 May 2014 at 05:34:19AM -0700, Pascal Germroth wrote:

> (def f1 "doc" ^{:x 1} (partial inc))
> (meta f1) ; nil, unexpected
> (meta #'f1) ; contains :doc, but not :x, as expected

The definition of partial is:

(defn partial
  ([f] f)
  ([f arg1]
   (fn [& args] (apply f arg1 args)))
  …)

So it just calls out to fn. fn is a macro, and returns metadata on
&form, i.e. the metadata on the literal (fn) form itself. Your ^{:x 1}
therefore is lost.

> (def f2 "doc" ^{:x 2} #(inc %))
> (meta f2) ; {:x 2}, as expected
> (meta #'f2) ; contains :doc, but not :x, as expected

#() returns an instance of FnReader at read-time, so metadata can be
attached as usual.

> (def f3 "doc" (with-meta (partial inc) {:x 3}))
> (meta f3) ; {:x 3}

> (def f4 "doc" (with-meta #(inc %) {:x 4}))
> (meta f4) ; {:x 4}

with-meta calls .withMeta on its argument at runtime, so everything that
implements IObj works.

What's interesting is that

    (meta ^:x inc) ; -> nil

does not work. I suppose this is because the reader cannot know if the
symbol `inc` represents an object that implements IMeta.

Hope this is accurate.

    guns

Attachment: pgpAjaltFssRA.pgp
Description: PGP signature

Reply via email to