2010/11/22 Mike K <mbk.li...@gmail.com> > In "Programming Clojure" Stuart Halloway says: > > It is important to note that the metadata reader macro is not the same > as with-meta. The metadata reader macro adds metadata for the > compiler, and with-meta adds metadata for your own data: > > (def ^{:testdata true} foo (with-meta [1 2 3] {:order :ascending})) > > (Note: example changed to 1.2 style syntax) > > He then shows the difference between (meta foo) and (meta #'foo). > > > However, it looks like I can accomplish the same thing with > > (def ^{:testdata true} foo2 ^{:order :ascending} [1 2 3]) > > So, is there really any difference between with-data and the reader > macro? >
Yes, but not in your example. The clojure documentation here http://clojure.org/reader (section "Macro characters") explains that you can attach metadata with ^ to "Symbols, Lists, Vector, Sets and Maps". So the example you use, using [1 2 3] which is a literal vector, will work with both ^ reader macro as well as (with-meta). Beyond what Nicolas pointed out in his answer, is also the fact that there are cases where trying to use (with-meta) instead of the ^ reader macro is "too late". Because with-meta will add meta data at runtime to an object, while the ^ reader macro adds meta data when the form is read from plain text, and thus the meta data is accessible to the compiler (the function eval). So type hints cannot be passed with with-meta. In fact, using them will not even be possible and compile: user=> (var-set (var *warn-on-reflection*) true) true user=> (defn f1 [s] (.length s)) Reflection warning, NO_SOURCE_PATH:41 - reference to field length can't be resolved. #'user/f1 user=> (defn f1 [^{:tag java.lang.String} s] (.length s)) #'user/f1 user=> (defn f1 [^String s] (.length s)) #'user/f1 user=> (defn f1 [(with-meta s {:tag java.lang.String})] (.length s)) java.lang.Exception: Unsupported binding form: (with-meta s {:tag java.lang.String}) (NO_SOURCE_FILE:44) user=> > > -- > 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<clojure%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- 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