I am struggling with type erasure: ```interface Ev...
# general-advice
a
I am struggling with type erasure:
Copy code
interface EventConsumer<EventType: DomainEvent> {
   fun handleEvent(event: EventType)
}

class MyConsumer: EventConsumer<Foo> {
   ...
}

fun main() {
  val consumer = MyConsumer()
  val events: List<DomainEvent> = ...
  events.forEach {
     consumer.handle(it)
  }
}
I want to find a way where only the events of type
Foo
are handled, but the rest are skipped. But I can't do something like this:
Copy code
interface EventConsumer<EventType: DomainEvent> {
  fun isRelevant(eventWithGlobalNr: EventWithGlobalNr<DomainEvent>): Boolean = EventType::class.isInstance(eventWithGlobalNr.event) // Cannot use 'EventType' as reified type parameter. Use a class instead.
}
k
In this case,
consumer.handleEvent(it)
would fail to compile because it only takes a
Foo
type.