-
-
Notifications
You must be signed in to change notification settings - Fork 725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
map & set multibinding #1951
base: 4.1.0
Are you sure you want to change the base?
map & set multibinding #1951
Conversation
Looks interesting, will check for 4.1 👍 |
abd0d70
to
38e0173
Compare
UPDATE: module {
intoMap("keyOfComponent1") {
Simple.Component1() // implicitly declare Map<String, Simple.Component1>
}
intoMap("keyOfComponent2") {
Simple.Component2() // implicitly declare Map<String, Simple.Component2>
}
intoSet {
Simple.Component1() // implicitly declare Set<Simple.Component1>
}
intoSet {
Simple.Component2() // implicitly declare Set<Simple.Component2>
}
/* Map<String, Simple.Component1> & Map<String, Simple.Component2>,
Set<Simple.Component1> & Set<Simple.Component2> are different multibindings */
} so I update the implement. Now elements can only be injected through explicit multibinding declaration and force type safety: module {
declareMapMultibinding<String, Simple.ComponentInterface1> {
intoMap("keyOfComponent1") { Simple.Component1() }
intoMap("keyOfComponent2") { Simple.Component2() }
intoMap("keyOfComponent3") { Other() } // build error
}
declareSetMultibinding<Simple.ComponentInterface1> {
intoSet { Simple.Component1() }
intoSet { Simple.Component2() }
intoSet { Other() } // build error
}
} |
b81de19
to
b2cf278
Compare
…ify the corresponding elements
b2cf278
to
35407ae
Compare
The later element definition overrides the previous one, and the elements are iterated in the order they are defined.
The rare use cases: data class MapKey(
val name: String,
val value: Int,
) {
override fun toString(): String = name
}
module {
declareMapMultibinding<MapKey, Simple.ComponentInterface1> {
intoMap(MapKey("key", 1)) { Simple.Component1() }
intoMap(MapKey("key", 2)) { Simple.Component1() } // MapMultibindingKeyTypeException will be thrown
}
} since multibinding implement uses val mapKey1 = MapKey("key", 1)
val mapKey2 = MapKey("key", 2)
module {
declareMapMultibinding<MapKey, Simple.ComponentInterface1> {
intoMap(mapKey1) { Simple.Component1() }
}
}
module {
declareMapMultibinding<MapKey, Simple.ComponentInterface1> {
intoMap(mapKey2) { Simple.Component1() } // NO exception thrown
}
}
val map: Map<MapKey, Simple.ComponentInterface1> = koin.getMapMultibinding()
map[mapKey1] == null
map[mapKey2] != null This is not the common use cases, maybe same module detection is enough? |
…ys have different values but the same `toString()` representation.
eed67d0
to
62535ab
Compare
The multibinding overrides issue: module {
declareMapMultibinding<String, Simple.ComponentInterface1> {
intoMap("keyOfComponent1") { Simple.Component1() }
}
}
module {
declareMapMultibinding<String, Simple.ComponentInterface1> { // multibinding override
intoMap("keyOfComponent1") { Simple.Component2() } // element override
}
} If we load modules with |
Implement map & set multibinding just like dagger2.
Here is some implementation details:
Every MapMultibinding & SetMultibinding will involve 3 definitions:
When getting specific element from map multibinding, the map multibinding will try to retrieve the element through multibindingValueQualifier;
When getting all elements from map or set multibinding, the multibinding will first try to get all MultibindingIterateKeys and filter them based on multibindingQualifier, and then retrieve each element through multibindingElementQualifier.
The known problems:
the elements order of multibiding is different with the order of element definitionsNow, both set & map multibinding elements are iterated in the order they are defined.And just like dagger2, multibinding is inherited (because MultibindingIterateKey can be retrieved from getAll), current scope's multibinding can get all elements that define in linked scope.
Here are some use cases:
issue #772