On Wed, Jan 27, 2010 at 8:52 AM, Jules <jules.gosn...@gmail.com> wrote:
>
> I have all the ctor arg types in my hand as well and went on to think
> that I could actually write a
>
> (defn make-ctor [class types] ...)
>
> which would return me :
>
>  (fn [#^type1 arg1 #^type2 arg2...#^typen argn]...)
>
> then pass this off as the to-be-called-frequently fn mentioned above,
> but my efforts to write this still all ended up returning a fn that
> used eval, as above......

Both eval and reflection could solve your problem, but for
maximum performance you want to avoid doing either inside your
tight loop.  I think you're on the right track with make-ctor,
but I'll call my make-factory, since it creates a fn that calls
ctors, not a fn that is a ctor:

  (defn make-factory [class & arg-types]
    (let [args (map #(with-meta (symbol (str "a" %1)) {:tag %2})
                    (iterate inc 1)
                    arg-types)]
      (eval `(fn [...@args] (new ~class ~...@args)))))

Note that to use it, you need to pass in symbols for classes, not
classes themselves:

  (def newInteger (make-factory 'Integer 'String))

Do that, and therefore the eval, outside your tight loop.  Now
you have a nice, fast, non-reflective factory function you can
call inside your tight loop:

  (newInteger "5")
  ;=> 5

Hope that helps.

--Chouser
http://joyofclojure.com/

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

Reply via email to