On Oct 23, 9:11 am, Paul Drummond
<[EMAIL PROTECTED]> wrote:
> I am a bit puzzled by how metadata seems to behave differently
> depeding on where it's used and whether #^ is included or not:
>
> (defn #^{:doc "doc"} my-fn ([x] x)) ;;Works
> (defn {:doc "doc"} my-fn ([x] x)) ;;Error: Second argument to def
> must be a Symbol
> (defn my-fn [x] x #^{:doc "doc"}) ;;Error: Unmatched delimiter: )
> (defn my-fn ([x] x) {:doc "doc"}) ; ;;Works
>
> Can anyone help me understand this please?
>
Sure. #^ is reader syntax for metadata. It adorns the following thing
read, it is not a thing unto itself. So,
#^{:doc "doc"} my-fn
is read all together as one thing, a symbol named my-fn, having the
metadata map {:doc "doc"}. Putting #^{:doc "doc"} at the end of a
list is metadata adorning nothing, and thus an error.
defn's internal syntax supports map literal forms, which are added as
metadata on the resulting var, but are not metadata literals in the
source.
The important thing to understand is how the reader works first,
producing a data structure which is them processed by any macros or
the compiler.
Read as list of symbol symbol list:
(defn #^{:doc "doc"} my-fn ([x] x))
Read as list of symbol map symbol list:
(defn {:doc "doc"} my-fn ([x] x))
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---