I wrote an answer for this problem when I needed to give a recursive function an optional parameter. I ended up with something like equivalent:
(defn func [a & [b?]]
(if b?
b?
(recur a a)))
My intention was to b?act as an optional parameter. If it was not sent, it will be by default nilthrough destructuring.
Instead of working, this gave me an error:
(func 1)
UnsupportedOperationException nth not supported on this type: Long clojure.lang.RT.nthFrom (RT.java:947)
After some debugging, I realized that for some reason, the rest parameter was not a list, as I expected, but simply a past number! The error arose because she tried to destroy the number.
I can fix this by getting rid of the wrapper list in the parameter list:
(defn func [a & b]
...
. , rest , b . "" , , :
(defn func2 [a & [b?]]
(if b?
b?
(func2 a a)))
(func2 1)
=> 1
- , ?