作者:qa32qw51 | 来源:互联网 | 2023-01-27 17:43
我和Dagger有一些奇怪的kotlin泛型问题,我有点固定,但解决方案并不合理.
这是匕首类:
@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()
当我构建时,我在顶级组件中得到了这个kapt错误:
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.(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 adapters(@org.jetbrains.annotations.NotNull()
显然不匹配,因为当我像这样修改匕首类时:
@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()
问题消失了.我是否因为协方差和逆变而陷入一些奇怪的翻译问题?我宁愿不强迫下游演员.
1> ephemient..:
Kotlin 默认List
转换为Java中的协方差List extends E>
.匕首不喜欢那样.
要解决此问题,您可以切换到MutableList
哪个是不变的,也可以写入List<@JvmSuppressWildcards E>
.