如何使用泛型类解决第二级继承?

时间:2019-01-22 10:07:36

标签: android generics inheritance kotlin polymorphism

我正在Kotlin中实现MVP架构,并且Android Studio没有显示错误,但是无法编译:

  

java.lang.AssertionError:ResultingDescriptor不应为null:   候选人描述符:构造函数BasePresenter(view:   视图)在com.exmaple.base.BasePresenter替换中定义:   org.jetbrains.kotlin.types.IndexedParametersSubstitution@70144d7e位于   org.jetbrains.kotlin.resolve.calls.model.ResolvedCallImpl.setResultingSubstitutor(ResolvedCallImpl.java:205)     ....

是否可以在继承过程中使用ConcreteView / ConcretePresenter

代码:

/**
 * These are the base classes
 */
interface MvpView

interface BaseView<Presenter : BasePresenter<*>> : MvpView, HasMvpView<MvpView>

// to avoid circular dependency
interface HasMvpView<View : MvpView> 

abstract class BaseMvpActivity<Presenter : BasePresenter<*>> : AppCompatActivity(), HasMvpView<MvpView>

abstract class BaseMvpFragment<Activity : BaseMvpActivity<*>, Presenter : BasePresenter<*>>: Fragment(), HasMvpView<MvpView> {
        // this will cause the error
        abstract var presenter: Presenter
}


/**
 * There's a Viewpager, and each page looks the same the only difference is their source of data. That's why I tried implementing another level of abstraction over the base classes
 */
interface CategoryPagerView<Presenter: BasePresenter<*>> : BaseView<Presenter>

abstract class CategoryPagerFragment: BaseMvpFragment<LandingActivity, BasePresenter<*>>()

/**
 * A concrete type of the Viewpager's fragment, its presenter and the view
 */
interface ConcreteView : CategoryPagerView<ConcretePresenter> 

class ConcretePresenter(view: ConcreteView) : BasePresenter<*>(view)

class ConcreteFragment : CategoryPagerFragment(), ConcreteView {
        // this line shows no error in the IDE, but in compile time
        override var presenter = ConcretePresenter(this)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

这是编译器抛出的内部声明错误。您可以在此处看到编译器源代码:

https://github.com/JetBrains/kotlin/blob/master/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallImpl.java

显然,resultingDescriptornull上永远是null。因此,这似乎是一个编译器错误。也许应该在Kotlin中将其重写以避免null出现问题:)

请在https://youtrack.jetbrains.com/issues/KT

提交错误报告