Aw: Creating a Clojure Record from a string
Hi, maybe you just want a factory? (defrecord Foo [a b]) (defn make-Foo [a b] (Foo. a b)) (defn make [^String class args] (let [last-dot (.lastIndexOf class \.) factory (ns-resolve (symbol (subs class 0 last-dot)) (symbol (str make- (subs class (inc last-dot)] (when factory (apply factory args))) Something like that? Sincerely Meikel -- 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
Creating a Clojure Record from a string
Given: test=(defrecord Foo [A B]) test=(class (Foo. 1 2)) test.Foo How do I: test=(new test.Foo 1 2) #:test.Foo{:A 1, :B 2} Currently I get Unable to resolve classname: test/Foo. Thanks, Timothy -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- 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
Re: Creating a Clojure Record from a string
Given: test=(defrecord Foo [A B]) test=(class (Foo. 1 2)) test.Foo How do I: test=(new test.Foo 1 2) #:test.Foo{:A 1, :B 2} Currently I get Unable to resolve classname: test/Foo. Thanks, Timothy (Class/forName java.lang.String) Be mindful of the performance... Stu Stuart Halloway Clojure/core http://clojure.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
Re: Creating a Clojure Record from a string
Given: test=(defrecord Foo [A B]) test=(class (Foo. 1 2)) test.Foo How do I: test=(new test.Foo 1 2) #:test.Foo{:A 1, :B 2} Currently I get Unable to resolve classname: test/Foo. Check out http://stackoverflow.com/questions/3748559/clojure-creating-new-instance-from-string-class-name. Be sure to scroll down to see all the replies, since the macro one is (IMO) the easiest to understand, although it has limitations. -- 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
Re: Creating a Clojure Record from a string
(Class/forName java.lang.String) Oh, does that work in 1.3? Because (new (Class/forName user.Foo)) was the first thing I tried (under 1.2) and it doesn't work. Perhaps unsurprisingly given that new is a special form. -- 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
Re: Creating a Clojure Record from a string
On Aug 23, 3:39 pm, Craig Andera cand...@wangdera.com wrote: (Class/forName java.lang.String) Oh, does that work in 1.3? Because (new (Class/forName user.Foo)) was the first thing I tried (under 1.2) and it doesn't work. Perhaps unsurprisingly given that new is a special form. No. But you can use reflection to futz around with the class object, find its constructor, and then invoke that. -- 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
Re: Creating a Clojure Record from a string
test= (def foo-class-symbol (load-string test.Foo)) test= (def foo (eval (list 'new foo-class-symbol 1 2))) test= foo #:test.Foo{:A 1, :B 2} Is that what you want? On Aug 23, 6:24 pm, Timothy Baldridge tbaldri...@gmail.com wrote: Given: test=(defrecord Foo [A B]) test=(class (Foo. 1 2)) test.Foo How do I: test=(new test.Foo 1 2) #:test.Foo{:A 1, :B 2} Currently I get Unable to resolve classname: test/Foo. Thanks, Timothy -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- 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