You've understood correctly - you'd need to create an instantiator for each 
different set of argument types.

Whether this suits you or not will depend on your use case: however in the 
(possible quite common?) situation that you are doing a lot of runtime 
instantiation using the same argument types then I think it is a big win.

You can also still define new-instance in terms of the instantiator if you 
like, using something like:

(defn new-instance "Create a new instance of the specified class with the 
given args."
 [^Class c & args]
  (apply (apply instantiator c (map class args)) args))

On Thursday, 4 July 2013 13:37:04 UTC+1, Jim foo.bar wrote:
>
>  hmm...unless I've understood wrongly, that would be 'once per type, 
> arity and potentially argument-types'...so yes, for all your Doubles you 
> would pay it once, for all your Longs  once more etc etc...if you start 
> doing it with objects with more than one ctor arity and possibly same 
> arities but with different arg-types then I don't see how the instantiator 
> would know about all potential possibilities. I mean it's not generic 
> anymore...
>
> have I completely misunderstood you? can you show a sample/overview of the 
> 'instantiator' fn you're referring to?
>
> thanks,
>
> Jim
>
>
> On 04/07/13 13:04, Mikera wrote:
>  
> Both are somewhat problematic from a performance perspective.
>
>  I'd actually be tempted to do this in a "higher order function" style 
> that returns a compiled construction function for the correct argument 
> types, e.g. something like
>
>  (def double-builder (instantiator Double Long))
> (double-builder 2)
> => 2.0 
>
>  This allows you to take the pay the cost of compilation / reflection 
> only once.
>
> On Thursday, 4 July 2013 11:57:32 UTC+1, 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 clo...@googlegroups.com <javascript:>
> Note that posts from new members are moderated - please be patient with 
> your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com <javascript:>
> 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+u...@googlegroups.com <javascript:>.
> 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