Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Clojure by

Hello! I have struggled to understand how to implement a multi-arity protocol method with a record, so I’d like to suggest an improvement to the defrecord docstring to make this clearer. I am thinking about this change (new is the last paragraph):

(methodname [args*] body)

The argument and return types can be hinted on the arg and
methodname symbols. If not supplied, they will be inferred, so type
hints should be reserved for disambiguation.

For multi-arity methods, add a separate (methodname ...) entry
for each one.*

1 Answer

0 votes
by

Isn't this implied by the stated syntax, given that it does not state a multi-arity syntax?

by
I don't think it is, given that `defprotocol` uses a different syntax and `fn`/`defn`/`letfn` all use another different syntax. The current documentation could also imply that you can't implement multi-arity functions (as it's not explicitly stated to support it).
by
I am sure that are folks that see that implied and are happy with that. But as you can see from my suggestion, I am not one of those sufficiently smart :-) and would have preferred more explicit info
by
lol I just ran into this when working on implementing IReduce and IReduceKV in basilisp (https://github.com/basilisp-lang/basilisp/pull/930). I forgot how to implement multi-arity protocols and couldn't understand why `(coll-reduce ([s f] (f)) ([s f init] init))` wasn't working. I even double checked the docstring. It wasn't until I looked at `clojure/core/protocols.clj` that I realized my mistake.
...