问题描述
我在Dagger中遇到了一些奇怪的Kotlin通用问题,我已经解决了,但是解决方案并不完善.
I'm having some weird kotlin generic issues with Dagger that I kinda fixed but the solution isn't sound.
这是匕首类:
@Module class P5Module {
@Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()
@Provides
fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)
: List<Pageable> = listOf(fusion, personas, skills, info)
}
@ActivityScope
@Subcomponent(modules = arrayOf(P5Module::class)) interface P5Component {
fun adapter(): PageableAdapter
}
interface Pageable {
fun manager(ctx: Context): LayoutManager
fun attach()
fun adapter(): Adapter<*>
}
class PageableAdapter
@Inject constructor(val pageables: List<Pageable>, val pool: RecyclerView.RecycledViewPool) :PagerAdapter()
构建时,在顶级组件中出现以下kapt错误:
When I build, I get this kapt error in the top level component:
e: C:\Users\daykm\StudioProjects\P5Executioner\app\build\tmp\kapt3\stubs\appDebug\com\daykm\p5executioner\AppComponent.java:17: error: [com.daykm.p5executioner.main.P5Component.adapter()] java.util.List<? extends com.daykm.p5executioner.view.Pageable> cannot be provided without an @Provides-annotated method.
e:
e: public abstract interface AppComponent {
e: ^
e: java.util.List<? extends com.daykm.p5executioner.view.Pageable> is injected at
e: com.daykm.p5executioner.view.PageableAdapter.<init>(pageables, …)
e: com.daykm.p5executioner.view.PageableAdapter is provided at
e: com.daykm.p5executioner.main.P5Component.adapter()
e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
我看了生成的存根:
@javax.inject.Inject()
public PageableAdapter(@org.jetbrains.annotations.NotNull()
java.util.List<? extends com.daykm.p5executioner.view.Pageable> pageables, @org.jetbrains.annotations.NotNull()
android.support.v7.widget.RecyclerView.RecycledViewPool pool) {
super();
}
@org.jetbrains.annotations.NotNull()
@dagger.Provides()
public final java.util.List<com.daykm.p5executioner.view.Pageable> adapters(@org.jetbrains.annotations.NotNull()
显然与之不匹配,因为当我像这样修改dagger类时:
Apparently doesn't match with because when I modify the dagger class like so:
@Module class P5Module {
@Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool()
@Provides
fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter)
: List<*> = listOf(fusion, personas, skills, info)
}
@ActivityScope
@Subcomponent(modules = arrayOf(P5Module::class)) interface P5Component {
fun adapter(): PageableAdapter
}
interface Pageable {
fun manager(ctx: Context): LayoutManager
fun attach()
fun adapter(): Adapter<*>
}
class PageableAdapter
@Inject constructor(val pageables: List<*>, val pool: RecyclerView.RecycledViewPool) :PagerAdapter()
问题消失了.我是否遇到了一些带有协方差和协方差的奇怪翻译问题?我宁愿不强制向下游投放演员.
The issue disappears.Am I bumping into some weird translation issue with covariance and contravariance? I'd rather not force a cast downstream.
推荐答案
在默认情况下,科特林的List<out E>
转换为Java List<? extends E>
中的协方差.匕首不喜欢那样.
Kotlin's List<out E>
translates to covariance in Java's List<? extends E>
by default. Dagger doesn't like that.
要解决此问题,您可以切换到不变的MutableList<E>
或编写List<@JvmSuppressWildcards E>
.
To get around this issue, you can either switch to MutableList<E>
which is invariant, or write List<@JvmSuppressWildcards E>
.
这篇关于为什么Dagger无法处理这些Kotlin泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!