I am trying to 'implement' the following Java interface from JGroups with reify
.
public interface MessageListener extends StateListener {
/**
* Called when a message is received.
* @param msg
*/
void receive(Message msg);
/** Called when a batch of messages is received */
default void receive(MessageBatch batch) {
for(Message msg: batch) {
try {receive(msg);}
catch(Throwable t) {}
}
}
}
In this case the receive method is present twice, with one parameter each (and different types, of course). When I try to reify
with the following code in CIDER:
(reify org.jgroups.MessageListener (receive[#^org.jgroups.Message msg]
(println msg)))
I get an error message:
CompilerException java.lang.IllegalArgumentException: Can't define method not in interfaces: receive, compiling:(*cider-repl clj-groups*:49:21)
As receive
is definitely available in the interface, I assume that this issue is related to the overloaded method and my typ hint is not working. What am I doing wrong?
Edit: After changing the code according to Lees comment:
(reify org.jgroups.MessageListener (receive[this #^org.jgroups.Message msg]
(println msg)))
the error message changed:
CompilerException java.lang.IllegalArgumentException: Mismatched return type: receive, expected: void, had: java.lang.Object, compiling:(*cider-repl clj-groups*:80:21)
this
parameter:(receive [this msg] ...)
. – Roofing^void
betweenreceive
and[
to type hint the return value. l don't know whether that will work. – Spermary