Hi. I'm implementing a toy ServiceLocator kind of ...
# announcements
a
Hi. I'm implementing a toy ServiceLocator kind of thing. Is it possible to achieve something akin to this via the type system?
Copy code
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
fun <T : Any> registerService(interface: KClass<T>, impl: T)
a
👌, 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>