On Dec 2, 11:15 pm, lazy1 <miki.teb...@gmail.com> wrote:
> Hello,
>
> I'm trying to create a "factory" method for Java classes, however I'm
> doing something wrong.
>
> (import '(java.util Dictionary HashMap))
>
> (def *containers* { :dict Dictionary :hash HashMap})
> (defn new-container
>   [type]
>   (new (*containers* type)))
>
> (def d (new-container :dict))
>
> The above gives me
> Exception in thread "main" java.lang.IllegalArgumentException: Unable
> to resolve classname: (*containers* type) (t.clj:6)

"new" does not evaluate its arguments, so it is trying to interpret
the form "(*containers* type)" as a classname, which it cannot do.

Another problem with your example is that java.util.Dictionary is an
abstract class, and cannot be instantiated directly; of the two
classes you gave, you will only be able to create instances of
HashMap.

...

> What is the right way to do this?

Aside from the issue with Dictionary being abstract, I think you need
a macro to do what you want to do:

(defmacro new-container [type]
  `(new ~(*containers* type)))

-Dave

>
> Thanks,
> --
> Miki

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