I'm Using gen-class to generate Java-classes from my Clojure code. In order to make gen-class work, I need to add an additional first parameter to all methods that will be added to the class (here called this).
(ns com.stackoverflow.clojure.testGenClass
(:gen-class
:name com.stackoverflow.clojure.TestGenClass
:implements [com.stackoverflow.clojure.TestGenClassInterface]
:prefix "java-"))
(def ^:private pre "START: ")
(defn java-addToString [this text post]
(str pre text post))
After compilation, calling the method in a Java-context works fine.
(def var (com.stackoverflow.clojure.TestGenClass.))
(.addToString var "Aus Clojure heraus (Methode 1)." " :ENDE")
(. var addToString "Aus Clojure heraus (Methode 2)." " :ENDE")
But how can I start it directly from Clojure?
Thge following does not work, since the first parameter is missing.
(java-addToString "TexT" " :END")
Is it good practice to simply call the function with an empty first parameter?
(java-addToString "" "TexT" " :END")
Or should I add a function (e.g. addToString) that I use internally and call this function from the one that will be added as a method to the class-file?
To want to treat the same code as a method on a generated class and as a function separate from said class seems a bit smelly to me (in the sense of code smell, something that indicates a problem somewhere in the design).
Perhaps
addToStringshould be a static method?Perhaps you should make an
add-to-stringfunction that can be used directly in clojure, and also called by theaddToStringmethod?We'd need to know more about your larger design before we know the best answer, but I suspect it is one of those.