![](/img/trans.png)
[英]error: [Dagger/MissingBinding] java.util.ArrayList<java.lang.Integer> cannot be provided without an @Inject constructor or an @Provides-annotated meth
[英][Dagger/MissingBinding].Kotlinjvm.functions.Function1<? super java.lang.Integer.Unit> cannot be provided
无法提供(Int) -> Unit
类型的kolin lambda
。 但是可以提供() -> Unit
:- 例如:-
@Module
class LambdaModule {
@Provides
fun getIntArgLambda(): (Int) -> Unit = {}
@Provides
fun getNoArgLambda(): () -> Unit = {}
@Provides
fun getRecyclerViewAdater(intLambda: (Int) -> Unit, noArg: () -> Unit): CustomAdapter = CustomAdapter(intLambda, noArg)
}
错误:-
[Dagger/MissingBinding] kotlin.jvm.functions.Function1<? super java.lang.Integer,kotlin.Unit> cannot be provided without an @Provides-annotated method.
但是,如果我不使用getIntArgLambda()
,它的工作原理:-
@Provides
fun getRecyclerViewAdater(noArg: () -> Unit): CustomAdapter = CustomAdapter({}, noArg)
上面的代码正在工作: -
为什么我不能为同一
module
中的任何方法提供(Int) -> Unit
参数?
这是一个方差问题。 它与 Java 通配符有关。
function:
@Provides
fun getIntArgLambda(): (Int) -> Unit = {}
在 Java 中返回:
kotlin.jvm.functions.Function1<java.lang.Integer, kotlin.Unit>
而function的参数intLambda
:
@Provides
fun getRecyclerViewAdater(intLambda: (Int) -> Unit, noArg: () -> Unit): CustomAdapter = CustomAdapter(intLambda, noArg)
在 Java 中是:
kotlin.jvm.functions.Function1<? super java.lang.Integer, kotlin.Unit>
要禁止通配符,您可以使用@JvmSuppressWildcards
:
@Module
class LambdaModule {
@Provides
fun getIntArgLambda(): (Int) -> Unit = {}
@Provides
fun getNoArgLambda(): () -> Unit = {}
@Provides
fun getRecyclerViewAdater(intLambda: Function1<@JvmSuppressWildcards Int, Unit>, noArg: () -> Unit): CustomAdapter = CustomAdapter(intLambda, noArg)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.