Is there a solid reason for "instantiate" not to be a plain old function
too? OK, the namespace the eval takes place in might change, but if that's
causing a problem with importing it seems that using (instantiate `MyClass
args...) should result in sending it the fully qualified classname, if it's
been imported in the namespace containing the call, rendering the eval
namespace issue moot.

On the other hand, is eval any faster than .newInstance here? It's actually
compiling a short piece of new bytecode and then invoking it. I wouldn't be
surprised if it was slower.

On Thu, Jul 4, 2013 at 7:31 AM, Robert Stuttaford <
robert.stuttaf...@gmail.com> 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.
>
>
>

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