In Clojure, how to use a java Class that is stored in a variable?
How should I fix the following code?
(def a java.lang.String)
(new a "1"); CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: a
And why this one works fine?
(def a str)
(a "1")
The most elegant solution is to write
constructthat does the same asnewbut is able to receive a class dynamically:This solution overcomes the limitation of @mikera‘s answer (see comments).
Special Thanks to @Michał Marczyk that made me aware of
invokeConstructoranswering another question of mine: Clojure: how to create a record inside a function?.Another option is to store the call to the constructor as an anonymous function. In our case: