Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


  1. How do you refer to a nested/inner class?
    Use: EnclosingClass$NestedClass
  2. Why doesn't (. Integer getName) work?
    . is a special operator. In particular, it does non-standard evaluation of its first arg. Because . can call static and instance methods, it determines if it is static call be seeing if the first arg names a class. In the case of (. Integer getName), it tries to find the static getName member and doesn't, because there isn't one. You can use this syntax to call a static method on Integer: (. Integer parseInt "42"). In short, if you can't say x.y() in Java you can't say (. x y) in Clojure, and you can't say Integer.getName() in Java.
  3. How do I call a Java method that takes a variable number of arguments?

    The variable arguments are actually just an array:

    Code Block
    {{(.method object fixed-args... (into-array type variable-args...))}}
  4. How do I get primitive types like int?

    Code Block
  5. When using gen-class, how do I specify which method to override based on type signature?
    This problem can appear if the class/interface you want to extend/implement defines methods with the same name and arity, only differing on the arguments types.
    The solution is to suffix the function name in clojure with the types names, separated by slashes.
    • Example1, the parent class/interface defines 2 methods

      Code Block
      add(int i1, int i2) and add(long l1, long l2)

      : In clojure you'll write:

      Code Block
      (defn -add-int-int [this i1 i2] ...) (defn -add-long-long [this l1 l2] ...)
    • Example2, the method to override has no argument, eg.

      Code Block

      . Then just append -void:

      Code Block
      (defn -print-void [this] ...)
    • Example3, case of a java array, specific syntax, eg. for overriding

      Code Block
      read(char[] cbuf, int off, int len)

      : You'll use the the-type<> syntax:

      Code Block
      (defn -read-char<>-off-len [this cbuf off len] ...)