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