android – 使用Dagger 2提供函数依赖

android – 使用Dagger 2提供函数依赖,第1张

概述我想使用Dagger 2提供一个函数作为依赖: @Moduleclass DatabaseModule { @Provides @Singleton fun provideDatabase(application: Application, betaFilter: (BetaFilterable) -> Boolean): Database { retu 我想使用Dagger 2提供一个函数作为依赖:

@Moduleclass DatabaseModule {    @ProvIDes    @Singleton    fun provIDeDatabase(application: Application,betaFilter: (BetaFilterable) -> Boolean): Database {        return Database(application,BuildConfig.VERSION_CODE,betaFilter)    }    @ProvIDes    @Suppress("ConstantConditionIf")    fun provIDeBetafiler(): (BetaFilterable) -> Boolean {        return if (BuildConfig.FLAVOR_audIEnce == "regular") {            { it.betaOnly.not() }        } else {            { true }        }    }}

不幸的是,它似乎不起作用:

[dagger.androID.AndroIDInjector.inject(T)] kotlin.jvm.functions.Function1<? super com.app.data.BetaFilterable,java.lang.Boolean> cannot be provIDed without an @ProvIDes-annotated method.

我在这里错过了什么?

解决方法 它不起作用,因为为了允许调用超类型的函数来代替lambda((Any) – >布尔值可以和(BetaFilterable) – >布尔)一起使用函数作为参数生成字节码到允许这个.

以下代码:

object Thingfun provIDe(): (Thing) -> Boolean {    Todo()}fun requires(func: (Thing) -> Boolean) {    Todo()}

结果如下签名:

signature ()Lkotlin/jvm/functions/Function1<LThing;Ljava/lang/Boolean;>;

declaration: kotlin.jvm.functions.Function1<Thing,java.lang.Boolean> provIDe()

signature (Lkotlin/jvm/functions/Function1<-LThing;Ljava/lang/Boolean;>;)V

declaration: voID requires(kotlin.jvm.functions.Function1<? super Thing,java.lang.Boolean>)

-LThing(?super Thing)和LThing(Thing)之间的细微差别使得这些类型与Dagger不兼容.

我不相信有可能使这项工作,你需要定义一个不相同的单独的接口?超/?像Function1一样扩展属性.

总结

以上是内存溢出为你收集整理的android – 使用Dagger 2提供函数依赖全部内容,希望文章能够帮你解决android – 使用Dagger 2提供函数依赖所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1121641.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存