On Mon, May 9, 2011 at 16:21, Jonathan Fischer Friberg
<[email protected]>wrote:
> Q1 This macro captures the correct namespace:
>
> (defmacro new* [type-name-as-symbol & args]
> `(clojure.lang.Reflector/invokeConstructor
> (ns-resolve ~*ns* ~type-name-as-symbol)
> (to-array '~args)))
>
Unfortunately that doesn't work -- it captures the namespace in which new*
is called, but that may not be where the symbol is actually written.
Consider this:
(ns my-namespace
(:refer dynamic-new))
(defrecord MyRecord [a b c])
(defn instantiate-using-parameter [type-name-as-symbol]
(new* type-name-as-symbol 1 2 3))
(in-ns 'another-namespace)
(my-namespace/instantiate-using-parameter 'MyRecord)
=> #:my-namespace.MyRecord{:a 1, :b 2, :c 3}
Again, the use of syntax-quote would qualify the symbol in the right
namespace -- the one containing the source of the symbol.
Simon
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en