General
- Do I need to know Java to use Clojure?
Not at first, but it helps to know the standard Java APIs.
Syntax
- Why doesn't
#(%)work?
Because it expands to(fnx(x)).#()always expands to include parens around the expression you give it. You might try#(vector x)instead.
Collections
- Why doesn't
nthwork on sets and maps?
"There is a difference between a sequential view of a collection (i.e. what you get from seq) and the collection itself. Maps are not sequential data structures. nth is only supported for sequential data structures, and that is by design." – nth on maps thread
Java Interop
- How do you refer to a nested/inner class?
Use:EnclosingClass$NestedClass - 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. - How do I call a Java method that takes a variable number of arguments?
The variable arguments are actually just an array: - How do I get primitive types like
int? - 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
add(int i1, int i2)andadd(long l1, long l2):(defn -add-int-intthis i1 i2...) (defn -add-long-longthis l1 l2...) - Example2, the method to override has no argument, eg.
print(). Then just append -void:(defn -print-voidthis...) - Example3, case of a java array, specific syntax, eg. for overriding
read(char[] cbuf, int off, int len):defn -read-char<>-off-lenthis cbuf off len...)
- Example1, the parent class/interface defines 2 methods
APIs
- How do I copy a file?
Useclojure.java.io/copy.
Lisp
- Does Clojure support custom reader macros?
No, and support is not planned.
Labels: