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

谁都会走 提交于 2020-01-28 08:55:48

问题


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.


回答1:


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.




回答2:


As answered Stuart Sierra in following thread, variadic methods aren't supported, and possibly will not supported in the future



来源:https://stackoverflow.com/questions/5401378/can-clojures-protocol-functions-be-variadic-like-ordinary-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!