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.

我在java中也做了同样的事情,而且效果很好.有人知道为什么会失败吗?

推荐答案

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.

您可以使用@JvmSuppressWildcards来避免它,如下所示:

@Inject lateinit var foo: Set<@JvmSuppressWildcards Foo>

Kotlin相关问答推荐

按钮无法在 Android Studio 上打开新活动

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

为什么记得不将 StateFlow 转换为特定类型?

如果不在可组合函数中,如何获取 stringResource

在子类中覆盖 kotlin 运算符扩展函数

通用接口继承

Kotlin 条件格式字符串

错误:cannot find symbol import com.gourav.news.databinding.ActivityDetailBindingImpl;

Kotlin:泛型、反射以及类型 T 和 T:Any 之间的区别

在 Kotlin 中通过反射获取 Enum 值

使用最新的 com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2 库时未找到 RxTextView 和其他小部件

Kotlin 枚举中的循环引用

如何在 Kotlin 中为变量设置监听器

runInTransaction 块内的挂起方法

如何使用Kotlin Dokka记录主构造函数参数

@uncheckedVariance 在 Kotlin 中?

如何在Kotlin中使方法param可变?

如何修复未解析的参考生命周期范围?

比较Kotlin的NaN

Kotlin中默认导入哪些包/函数?