2017-07-29 71 views
1

我有一些奇怪的kotlin與Dagger的一般問題,我有點固定,但解決方案不健全。爲什麼不能匕首處理這些kotlin仿製藥?

這裏的匕首類:

@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錯誤在頂級組件:

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() 

顯然不符合,因爲當我修改匕首類如下:

@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

哎呀,直到我寫了我的答案後才發現,但https://stackoverflow.com/a/43149382稍微詳細一點。 – ephemient

回答

6

默認情況下,Kotlin的List<out E>轉換爲Java的List<? extends E>的協方差。匕首不喜歡那樣。

要解決此問題,您可以切換到不變的MutableList<E>或寫入List<@JvmSuppressWildcards E>