The HOF can achieve very good performance if by caching the constructor 
object.  Just be careful to clear the cache when the class is GC'd, 
otherwise you can end up with memory leaks and/or class cast exceptions.

On Thursday, July 4, 2013 4:31:40 AM UTC-7, Robert Stuttaford wrote:
>
> Looks like the tradeoffs are composability vs avoiding reflection. I 
> personally would go for the function until performance becomes a concern. 
> Being able to use it in HOFs is a big deal!
>
> On Thursday, July 4, 2013 12:57:32 PM UTC+2, Jim foo.bar wrote:
>>
>>  (defmacro instantiate 
>> "Returns an instance of the given class. Depending on the argument list will 
>> invoke the corresponding constructor." 
>> [cl-name & args]
>>  `(eval (list 'new ~cl-name ~@args)))
>>  
>> (defn new-instance "Create a new instance of the specified class using 
>> reflection."
>>  [^Class c & args]
>>   (if (empty? args) (.newInstance c)
>>     (.newInstance
>>       (.getConstructor c (into-array (map class args)))
>>       (to-array args))))
>>
>>
>> any particular preference? 
>>
>> Jim
>>
>>  

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to