> I have a Clojure applications with different files, different
> namespaces and I have imports of classes within those files/
> namespaces.
I don't think the following is exactly what you're asking for, but you
can use "clojure.contrib.core/new-by-name" to do something like what
you described.
Here's an example from clojure.contrib.miglayout.internal:
(def MigLayout "net.miginfocom.swing.MigLayout")
[...]
(.setLayout (new-by-name MigLayout layout column row))
[...]
Using this technique I was able to change clojure.contrib.miglayout's
dependence on the miglayout jar from compile time to runtime.
--Steve
Now, that is close, not entirely but close enough.
Do you think there will be any performance hits.
I think that's correct. In my experience a call that
requires runtime reflection like this is on the order of 50
times slower than a method call that is made directly.
Depending on how often it gets called, I think that would
generally count as a "performance hit".
Another option you could consider: moving all code that
requires that class into a separate .clj file, then using
'load' when you need it. This would allow you to AOT
compile (or not) and suffer none of the runtime reflection
cost.
Yet another option would be to eval a constructor function.
Something like:
(defn make-foo [arg1 arg2]
(eval `(defn ~'make-foo [a1# a2#] (Foo. a1# a2#)))
(make-foo arg1 arg2))
Note that's not well tested. I'm not sure, but you may have
trouble elsewhere if you try to type-hint Foo. But
basically, that defines a make-foo such that the first time
it's called it compiles a new make-foo that will actually
create instances of Foo. This will be slow, but subsequent
calls to make-foo should run at full compiled speed.
Both those options may be pretty painful depending on what
you're actually doing, so if I were you I'd try new-by-name
first and only if you're sure it's too slow would I try
another option.
--Chouser