kotlinextension-methodstype-parameter

Is there any difference in Kotlin between a capped type parameter extension function and a normal extension function?


Is there any difference between these two?

fun <T : Parent> T.function() {}

vs.

fun Parent.function() {}

Solution

  • As you wrote it - no.

    It is important for function chaining:

    fun <T : Parent> T.function():T { return this } //allows chaining
    

    vs.

    fun Parent.function():Parent { return this } //casts to base class