I am using Guava’s EventBus in my Scala project.
I have a parameterized event like so:
class MyEvent[T]
And a simple event listener:
class MyEventListener {
@Subscribe
def onStringEvent(event: MyEvent[String]) {
println("String event caught")
}
@Subscribe
def onIntEvent(event: MyEvent[Int]) {
println("Int event caught")
}
}
I can create my com.google.common.eventbus.EventBus, register MyEventListener, and fire an event:
val eventBus = new EventBus
eventBus.register(new MyEventListener)
eventBus.post(new MyEvent[String])
But, as you may have guessed already, both onStringEvent and onIntEvent get called as a result. The issue is that Java’s/Scala’s type erasure drops off the parameter type at runtime and both subscriptions appear to Guava as event: MyEvent.
Ok, my question:
Due to erasure, using the same Event object for different types of Guava events in this manner wouldn’t be possible in Java and isn’t possible in Scala. However, Scala proves to have a number of nice ways to circumvent Java’s erasure problems. Does anybody see another way to achieve this, perhaps using some Scala wizardry?
The problem is in Guava: it cannot see the type parameter, and so it will not distinguish between the two methods. The only possible solution is to create a new class for each type.
That can be really easy:
and then whenever you need to do anything in your code, just use
MyEvent[Int]. But Guava will require at least this much boilerplate.Note that I’ve made the
MyEvent[T]constructor protected so you have to instantiate one of the de-generified classes. I’m not sure whether that will work for your use-case; I’ll assume so. You can get around that also (with type classes), but it adds more boilerplate.