Generics question, I’ve defined an interface for m...
# getting-started
z
Generics question, I’ve defined an interface for my factories and I’m trying to create a mapping so I can pass in the class of an Event & get back the factory which can create it.
Copy code
interface SchemaFactory<in E: Event, out S: Schema> {

  fun create(event: E) : S
}

class TestEventFactory : SchemaFactory<TestEvent, TestEventSchema>

private val factories: Map<Class<out Event>, SchemaFactory<Event, Schema>> = mapOf(TestEvent::class.java to TestEventFactory())
However I get the following error message on `factories`:
Copy code
Type mismatch.
Required:
Map<Class<out Event>, SchemaFactory<Event, Schema>>
Found:
Map<Class<out Event>, TestEventFactory>
how would I define a map of factories that can only take in an event which matches their key in the map?
interesting, I’ll give this a shot. thank you!
is there a safer approach to this? seems like a somewhat common usecase
I see, I guess I meant is there a standard approach to what I’m trying to do
fair enough! thank you for your time and all this detail 🙂
e
there are type-safe maps, but they can't be
Map
CoroutineContext/.Element/.Key is one of them in stdlib