Geza
Geza

Reputation: 45

How to solve 2nd level inheritance with generic classes?

I'm implementing an MVP architecture in Kotlin and Android Studio shows no error, however it doesn't compile:

java.lang.AssertionError: resultingDescriptor shouldn't be null: candidateDescriptor: constructor BasePresenter(view: View) defined in com.exmaple.base.BasePresenter substitution: org.jetbrains.kotlin.types.IndexedParametersSubstitution@70144d7e at org.jetbrains.kotlin.resolve.calls.model.ResolvedCallImpl.setResultingSubstitutor(ResolvedCallImpl.java:205) ....

Is it possible to use the ConcreteView / ConcretePresenter in the middle of inheritance?

code:

/**
 * 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)

Thanks for your help!

Upvotes: 3

Views: 278

Answers (1)

herman
herman

Reputation: 12305

It's an internal assertion error thrown by the compiler. You can see the compiler source code where this is thrown:

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

Apparently the resultingDescriptor is null at a point where it should never be null. So it seems like a compiler bug. Maybe it should be rewritten in Kotlin to avoid issues with null :)

Please file a bug report at https://youtrack.jetbrains.com/issues/KT

Upvotes: 1

Related Questions