-
-
Notifications
You must be signed in to change notification settings - Fork 143
Description
Currently, the DestinationDependenciesContainerImpl class uses a dependency function that requires both the dependency instance and a KClass, which is used as the map key.
fun <D : Any> dependency(dependency: D, asType: KClass<in D>) {
map[asType.java] = dependency
}However, when consuming the library, only the dependency instance can be provided and the key is derived internally from its type. The option to specify a key is internal to the implementation. This design leads to key collisions when multiple interfaces share the same base type: the resolved key becomes the common supertype, causing entries to overwrite each other in the map.
inline fun <reified D : Any, T> DependenciesContainerBuilder<T>.dependency(dependency: D) {
(this as DestinationDependenciesContainerImpl<*>).dependency(dependency, asType = D::class)
}In my case, I bind multiple navigatos into a map via DI and need to pass the small collection as dependencies. This is not possible with the current API, because the set’s runtime type defaults to Any::class, resulting in all navigators sharing the same key. Consequently, only a single navigator can be stored and retrieved from the map, unless you add them all by hand as dependencies.
We can keep the same type as key with a default value, but allow another key specified by the consumer. Or even allow other key types than only KClass.
inline fun <reified D : Any, T> DependenciesContainerBuilder<T>.dependency(dependency: D, key: KClass<out Any> = D::class) {
(this as DestinationDependenciesContainerImpl<*>).dependency(dependency, asType = key)
}