Kotlin循环泛型

时间:2019-01-02 05:42:14

标签: android generics kotlin

我有3类Router,Interactor和Component,在Java中,我有

Interactor<R extends Router>
Router<C extends InteractorComponent, I extends Interactor>
Component<I extends Interactor>

当我转换为Kotlin时会得到

Interactor<Self : Interactor<Self, R>, R> :
LifecycleScopeProvider<InteractorEvent> where R : Router<R, *, Self>

Router<Self : Router<Self, C, I>, in C, I>(
component: C,
private val interactor: I) where C : Component<I>, I : Interactor<I, Self>

Component<I : Interactor<I, *>>

但是在某些子类中,Router-> ViewRouter(仅添加一个视图)-> PagerViewRouter却更深入了

PagerViewRouter<V, C, I>(
view: V,
component: C,
interactor: I) : ViewRouter<PagerViewRouter<V, C, I>, V, C, I>(
view,
component,
interactor) where V : View, C : Component<I>, I : ViewInteractor<I, PagerViewRouter<V, C, I>, *>

当我尝试声明这些PagerViewRouters的列表(每个列表可以是不同的具体类型)时,我收到ViewInteractor类型不在范围内的错误

MutableList<PagerViewRouter<
        out View,
        out InteractorComponent<out ViewInteractor<*, *, *>>,
        out ViewInteractor<*, *, *>>>

由于该Interactor之类的ViewInteractor引用Self作为第一个通用对象,因此更新错误基本上会导致无限更新

如何在保持类型安全的同时降低这种复杂性并避免循环引用?

0 个答案:

没有答案