On Fri, Jan 16, 2009 at 9:57 PM, David Nolen <dnolen.li...@gmail.com> wrote:
> Just made sense to me today as well.
> #^Class
> is short form for saying set the metadata for the symbol being defined (in
> this case list) to the map {:tag Class}.
> #^ is a reader macro for setting metadata for the compiler.  That code is
> simple tagging the symbol clojure/list.
> (meta #'list)
> will give you the map representing the metadata.  :tag is used by the
> compiler, but it's not clear to me in what cases...
>

The most common case where :tag is used by the compiler is to generate
direct method calls into Java code rather than using reflection to do
the call, which is much slower, e.g:

(defn index-of [s substr]
  (.indexOf s substr))

The call to String.indexOf will likely be compiled as a reflective
call because in the general case, the compiler doesn't know that 's'
and 'substr' are meant to be instances of java.lang.String. If however
you add a couple of type hints:

 (defn index-of [#^String s #^String substr]
   (.indexOf s substr))

Then the compiler will generate an optimized code path that directly
invokes String.indexOf(String,String), and the other which will fall
back to the reflective invocation. The reason that two code paths are
needed is because Clojure is dynamically typed. Therefore it is
perfectly legal to pass in any type of objects as the parameter in
which case you want the call to .indexOf to succeed if the passed in
instance does in fact have a method named indexOf that can accept
'substr' as its argument.

To see where the compiler needs to help you can use the
*warn-on-reflection* flag to ask for warnings where the compiler can't
resolve the calls. See:

 http://clojure.org/java_interop#toc35

for example usage.

/mike.

--~--~---------~--~----~------------~-------~--~----~
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
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