On 03/09/2015 15:45, Juvenn Woo wrote: > Hi all, > > I am writing a function that'll take a java class name as an arg, > wherein I'll make instance of the class. Several approaches did I try: > > (let [klass Integer] (new klass 42)) ; this raises exception "unable to > resolve symbol: klass" > > (let [klass Integer] (.new klass 42)) ; raises "no matching method > found: new for class java.lang. Class" > > I'm running out of ideas currently. How do you deal the use case like > this? And what's the idiomatic way to do this?
You can use Java reflection for this or use the Reflector from Clojure: (let [klass Integer] (clojure.lang.Reflector/invokeConstructor klass (into-array [42]))) -- 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/d/optout.