Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chained calls and inheritance

Tags:

kotlin

I have a following class hierarchy

open class B {
    fun b() = this
}

open class C : B() {
    fun c() = 0
}

and I would like to do something like this:

fun test(c: C) {
    c.b().c() // error: c.b() returns B, not C
}

I understand why such problem exists in Java, but Kotlin has smart casts, so why compiler can't smart cast c.b() to C in this situation?

Are there any workarounds better than the Java ones in Kotlin to make chained calls work correctly with inheritance?

like image 590
netimen Avatar asked Sep 15 '25 08:09

netimen


1 Answers

Smart cast is not applicable here, since compiler has no way to understand that B#b() returns an instance of C.

The fact that the returned B instance is a C is obvious for us, humans, but only after we see the implementation of the method. Type inference can only work with function signatures, so all it sees is fun b(): B = ...

A simple solution for your case would be to make b() a generic extension:

open class B 

fun <T: B> T.b() = this

open class C : B() {
    fun c() = 0
}
like image 101
voddan Avatar answered Sep 16 '25 21:09

voddan