上限类型参数扩展函数和普通扩展函数之间在 Kotlin 中有什么区别吗?
Is there any difference in Kotlin between a capped type parameter extension function and a normal extension function?
这两个有什么区别吗?
fun <T : Parent> T.function() {}
对比
fun Parent.function() {}
如您所写 - 不。
对于函数链接很重要:
fun <T : Parent> T.function():T { return this } //allows chaining
对比
fun Parent.function():Parent { return this } //casts to base class
这两个有什么区别吗?
fun <T : Parent> T.function() {}
对比
fun Parent.function() {}
如您所写 - 不。
对于函数链接很重要:
fun <T : Parent> T.function():T { return this } //allows chaining
对比
fun Parent.function():Parent { return this } //casts to base class