我已经开始尝试使用kotlin并出现了一个问题 我已经为可变列表声明了扩展属性并试图在字符串模板中使用它:Kotlin扩展属性无法在字符串模板中识别
fun main(args: Array<String>) {
val list = mutableListOf(1,2,3)
// here if use String template the property does not work, list itself is printed
println("the last index is $list.lastIndex")
// but this works - calling method
println("the last element is ${list.last()}")
// This way also works, so the extension property works correct
println("the last index is " +list.lastIndex)
}
val <T> List<T>.lastIndex: Int
get() = size - 1
,我已经得到了以下输出
the last index is [1, 2, 3].lastIndex
the last element is 3
the last index is 2
第一的println输出预计将与第三之一。我试图获得模板中列表的最后一个元素,它工作正常(第二个输出),所以是一个错误或我缺少一些使用扩展属性时的东西?
我使用科特林1.0.5
谢谢,它的工作!公认。 – lopushen