下面的多重绑定有效,当提供Pair
作为IntoSet
时>
@Provides @IntoSet fun entryOne(): Pair<String, String> { val key = randomStringGenerator() val value = "Random Value 1" return Pair(key, value) } @Provides @IntoSet fun entryTwo(): Pair<String, String> { val key = randomStringGenerator() val value = "Random Value 2" return Pair(key, value) } @Provides fun randomKeyValueMap(entries: Set<Pair<String, String>>): Map<String, String> { val randomKeyValueMap = LinkedHashMap<String, String>(entries.size) for (entry in entries) { randomKeyValueMap[entry.first] = entry.second } return randomKeyValueMap }
但是将
Pair
变成SimpleEntry
时,它不再起作用。
@Provides @IntoSet fun entryOne(): AbstractMap.SimpleEntry<String, String> { val key = randomStringGenerator() val value = "Random Value 1" return AbstractMap.SimpleEntry(key, value) } @Provides @IntoSet fun entryTwo(): AbstractMap.SimpleEntry<String, String> { val key = randomStringGenerator() val value = "Random Value 2" return AbstractMap.SimpleEntry(key, value) } @Provides fun randomKeyValueMap(entries: Set<AbstractMap.SimpleEntry<String, String>>): Map<String, String> { val randomKeyValueMap = LinkedHashMap<String, String>(entries.size) for (entry in entries) { randomKeyValueMap[entry.key] = entry.value } return randomKeyValueMap }
投诉
error: [Dagger/MissingBinding] java.util.Set<? extends java.util.AbstractMap.SimpleEntry<java.lang.String,java.lang.String>> cannot be provided without an @Provides-annotated method. public abstract interface MyComponent { ^ java.util.Set<? extends java.util.AbstractMap.SimpleEntry<java.lang.String,java.lang.String>> is injected at
注意,如果我将
Entry
用于Java,它可以正常工作。仅不适用于Kotlin。
下面的多重绑定有效,当提供对作为IntoSet @Provides @IntoSet fun entryOne()时:Pair