Kotlin:嵌套未命名函数中的此表达式

Kotlin:嵌套未命名函数中的此表达式,kotlin,Kotlin,Kotlin文档描述了如何在嵌套类中进行访问 是否可以在嵌套的未命名函数中访问此表达式?e、 g: str = "abcde" str.run {

Kotlin文档描述了如何在嵌套类中进行访问

是否可以在嵌套的未命名函数中访问此表达式?e、 g:

str = "abcde"
str.run {                                                                                                                                                                                              
this.substring(0..2).run {                                                                                                                                                                         
    println("${this} is a substring of ${???}")                                                                                                                                                    
}

为了回答所问的问题,在这种情况下可以提高可读性,您可以:

通常,您会使用隐式标签。例如,如果外部调用更改为使用与内部调用不同的函数名,则可以访问它:

with (str) {
    this.substring(0..2).run {
        println("${this} is a substring of ${this@with}") // Use the implicit label
    }
}
with (str) {
    this.substring(0..2).run {
        println("${this} is a substring of ${this@with}") // Use the implicit label
    }
}