Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Clojure's protocol functions be variadic like ordinary functions?

With clojure functions, I can define:

(defn f [x & xs] (apply some-function x xs))

I'm attempting to do this same sort of thing with a protocol, e.g.

(defprotocol foo
  (bar [f])
  (baz [f & gs]))

This compiles (at least in the REPL), but any implementing type seems to fail on this (the variadic, baz) method. Is this officially not supported? The sources that I've consulted are silent.

like image 278
Rob Lachlan Avatar asked Mar 23 '11 05:03

Rob Lachlan


1 Answers

This is not supported, for the reasons Stuart Sierra gives. To go into a little more detail, the & symbol is special only in a destructuring context like let or function arguments. As Stuart makes clear, defprotocol is not such a context.

But & is still a legal symbol, so you've defined a protocol with two functions: bar takes one argument, named f, and baz takes three, named f, &, and gs.

like image 124
amalloy Avatar answered Oct 10 '22 02:10

amalloy