[CLJ-735] Improve error message when a protocol method is not found Created: 04/Feb/11 Updated: 04/May/15
|Affects Version/s:||Release 1.2|
If you call a protocol function but pass the wrong arity (forget an argument for example), you currently a message that says "No single method ... of interface ... found for function ... of protocol ...". The code in question is getting matching methods from the Reflector and creates this message if the number of matches != 1.
There are really two cases there:
I propose that the == 0 case instead should have slightly different text at the beginning and a hint as to the intended arity within it:
"No method: ... of interface ... with arity ... found for function ... of protocol ...".
The >1 case should have similar changes: "Multiple methods: ... of interface ... with arity ... found for function ... of protocol ...".
Patch is attached. I used case which presumably should have better performance than a nested if/else. I was not sure whether the reported arity should match the actual Java method arity or Clojure protocol function arity (including the target). I did the former.
I did not add a test as I wasn't sure whether checking error messages in tests was appropriate or not. Happy to add that if requested.
|Comment by Chas Emerick [ 14/Jul/11 6:39 AM ]|
I think it should be the latter. The message is emitted when the protocol methods are being invoked through the corresponding function, so it should be consistent with the errors emitted by regular functions.
+1 for some tests, too. There certainly are tests for reflection warnings and such.
FWIW, I'm happy to take this on if Alex is otherwise occupied.
|Comment by Alex Miller [ 04/May/15 4:22 PM ]|
Picking this up a zillion years later ...
1) I think I would now change the control flow slightly to make the normal path (methods.size() == 1) first and the error cases after that without that switch/case stuff:
2) The Compiler code should use tabs instead of spaces.
3) I would like tests added for the error messages in these two cases. Those can go in clojure.test-clojure.protocols.