Fluid Dynamics <a2093...@trbvm.com> writes:

> On Saturday, January 24, 2015 at 1:43:15 PM UTC-5, Elric Erkose wrote:
>>
>> Hello,
>>
>> I have an app which specifies a protocol and a record name. At runtime, it 
>> searches the classpath for files implementing the specification and creates 
>> a map of namespace to instance for each. Before I implemented the protocol, 
>> I just specified function names. I was able to lookup the function names 
>> and call them. After I switched to using a protocol, I get a 
>> CompilerException.
>>
>> Here's the question: How do I instantiate a known record from an unknown 
>> namespace?
>>
>> Here is the sample code. Thanks for you help.
>>
>> ```clojure
>> (ns proto)
>>
>> (defprotocol Proto
>>   (meth [this]))
>>
>> ;; use clojure.tools.namespace.find to locate the namespace.
>> ;; for simplicity, we just def it here.
>> (def that 'rec)
>>
>> (require that)
>>
>> ;; the symbol "Rec" is part of the specification
>> (let [inst (ns-resolve that (symbol "Rec"))]
>>   (when (class? inst)
>>     (println "inst:" (type inst) inst)
>>     ;; => java.lang.Class rec.Rec
>>     
>>     (println "meth:" (meth (new rec.Rec)))
>>     ;; => it works when I know it.
>>
>>     ;; uncomment this for a CompilerException
>>     ;;(println "meth:" (meth (new inst)))
>>     ;; => CompilerException java.lang.IllegalArgumentException:
>>     ;;      Unable to resolve classname: inst, compiling:(proto.clj:21:28)
>>     ))
>> ```
>>
>> ```clojure
>> (ns rec
>>   (:require proto))
>>
>> (defrecord Rec []
>>   proto.Proto
>>   (meth [this] "it works when I know it"))
>> ```
>>
>
> The new special form doesn't evaluate its argument, so "inst" results in 
> looking for a classname of "inst", not whatever inst is bound to. (eval 
> `(new ~inst)) might work though. 
>
> -- 
> 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.


Using `eval' here is like shooting sparrows with cannons. While I
despise the latter and wouldn't help with that, a better solution for
the former is the `newInstance' method of java.lang.Class:

(let [c java.lang.String]
  (.newInstance c)) 
; => ""

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

Attachment: signature.asc
Description: PGP signature

Reply via email to