Hello everyone,

I came upon a weird behaviour of Clojure. Weird as far as I understand
what is going on with metadata. Maybe not very far.

I understand that the reader macro #^ associates metadata with a form,
for use by macros or the compiler as illustrated below.

user=> (defmacro m [form] `(println (meta (quote ~form))))
#'user/m
user=> (m #^{:foo "bar"} '(1 2 3))
{:line 7, :foo bar}
nil
user=> (m #^{:foo "bar"} x)
{:foo bar}
nil

I think using a macro like m is the only way to get the metadata
associated with the symbol because symbols are different everytime we
construct one, even if their spelling is the same. That explains why
(defn ff [#^Integer x] (println (meta 'x)) x) fails to display the
metadata associated with formal argument x.

But when I define a function with defn augmenting the definition with
metadata, I observe two things I don't understand. I will comment on
the code below to illustrate my problem.

mac:clojure ludo$ clj
Clojure 1.2.0-master-SNAPSHOT
user=> (defn #^{:doc "doubles argument" :tag Integer} f [#^Integer x]
(* x 2))
#'user/f
user=> (meta f)
{:ns #<Namespace user>, :name f}
user=> (defn #^{:doc "doubles argument" :tag Integer} f [#^Integer x]
(* x 2))
#'user/f
user=> (meta f)
{:ns #<Namespace user>, :name f, :file "NO_SOURCE_PATH", :line
1, :arglists ([x]), :doc "doubles argument", :tag java.lang.Integer}
user=> (meta #'f)
{:ns #<Namespace user>, :name f, :file "NO_SOURCE_PATH", :line
3, :arglists ([x]), :doc "doubles argument", :tag java.lang.Integer}

The first defn defines a fuction called f with metadata provided by
the reader for the symbol f and the symbol x. Thus the special form
def (hidden behind the macro defn) takes the metadata and associates
it with the var #'f.

1) When I call (meta f), I get something. I thought there can't be any
metadata on a function value. But I see :ns and :name in the map.

2) If a redefine f with the same call to defn, I get full metadata on
the value of f as well as #'f. As meta is an ordinary function, the
argument f gets evaluated, producing a function value and then I
retrieve the metadata associated with the value and get the whole
stuff as if I called it on #'f.

Point 1 is weird but point 2 is even weirder.

I would be thankful if anyone could provide insights on this problem.

Ludovic Kuty

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to