Dagger 2 multibindings with Kotlin

2020-02-05 01:23发布

I have the following snippet in my dagger 2 module

@Singleton
@Provides
@ElementsIntoSet
fun providesQueries(foo: Foo): Set<Foo>{
    val queries = LinkedHashSet<Foo>()
    queries.add(foo)
    return queries
}

I try to inject into in this way

@Inject lateinit var foo: Set<Foo>

But dagger shows an error which says that Dagger cannot provides java.util.Set without @Provides or @Produces method.

I did the same in java and it worked. Does somebody know why is it failing?

1条回答
Ridiculous、
2楼-- · 2020-02-05 01:48

As it described in the Kotlin reference

To make Kotlin APIs work in Java we generate Box<Super> as Box<? extends Super> for covariantly defined Box (or Foo<? super Bar> for contravariantly defined Foo) when it appears as a parameter.

You can use @JvmSuppressWildcards for avoiding it, just as following:

@Inject lateinit var foo: Set<@JvmSuppressWildcards Foo>
查看更多
登录 后发表回答