How to use optional arguments in defprotocol?
clojure, java
Solution
As answered here, protocols do not support varargs. You have to define a separate method for each required number of arguments or simply accept a list as an argument.
Problem
I want to have a function in my type which takes an optional argument. I have successfully used optional arguments in functions which do not belong to a type, however, I cannot seem to get it working within a protocol and type. My failed trial: ``` Clojure 1.4.0 user=> (defprotocol OptArg (func [this a & b])) OptArg user=> (deftype Imp [] OptArg (func [this a & b] (println a b))) user.Imp user=> (func (Imp.) 1 2) CompilerException java.lang.IllegalArgumentException: No single method: func of interface: user.OptArg found for function: func of protocol: OptArg, compiling:(NO_SOURCE_PATH:5) user=> (defn opt-arg [a & b] (println a b)) #'user/opt-arg user=> (opt-arg 1 2) 1 (2) nil user=> (opt-arg 1) 1 nil nil user=> ```