I've got pretty simple java code:
public class MessageListenerExample extends ListenerAdapter
{
@Override
public void onMessageReceived(MessageReceivedEvent event)
{
// do something with event
}
}
However, I can't seem to understand how to turn that code into clojure code. The docs and articles are very confusing. I'd be happy to see more examples too. I'm also interested in using implements
.
You can use
proxy
to extend an existing Java class and also implement interfaces. For example:Just implement
EventListener
yourself, instead of using the adapter class that's there to make things more convenient for java programmers (but makes it harder for clojure programmers!). You'll receive an Event object, and you can check for yourself whether it's an instance ofMessageReceivedEvent
, just like the adapter would do for you.Implementing an interface in Clojure is done via
reify
- see https://stackoverflow.com/a/8615002/625403 for an example.Depending on what you need to do, a few options:
I wouldn't recommend going down this path unless absolutely necessary. Getting compilation right (including AOT compilation) is tricky. And in the end you still need to use Java interop to work with the objects of this class, so not sure it's worth the hassle, which brings me to:
Code it in Java and use Java interop to work with it.
If you actually need to create an instance of an object that implements a certain interface, then it's easier:
I use it around my code, it's really much nicer that coding in Java.