匕首2集多重绑定不适用于Kotlin中的SimpleEntry吗?

问题描述 投票:0回答:1

下面的多重绑定有效,当提供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 {val key = randomStringGenerator()val value =“ ...

kotlin dagger-2 multibinding
1个回答
0
投票
看起来我需要@JvmSuppressWildcards
© www.soinside.com 2019 - 2024. All rights reserved.