https://kotlinlang.org logo
Title
a

arve

12/12/2018, 11:13 AM
Hi. I'm implementing a toy ServiceLocator kind of thing. Is it possible to achieve something akin to this via the type system?
fun registerService(interface: ???, impl: ???) { ... }

registerService(UserInterface::class, UserService()) // ok
registerService(CarInterface::class, CarService()) // ok
registerService(ChairInterface::class, TableService()) // fail, because TableService does not implement ChairInterface
d

diesieben07

12/12/2018, 11:45 AM
fun <T : Any> registerService(interface: KClass<T>, impl: T)
a

arve

12/12/2018, 12:05 PM
👌, thanks!
Can I extend this constraint to my MutableMap serviceCache as well, I wonder.. 🤔 Or would I have to be content with Map<Any, Any>