kotlin_作用域函数run{}

文章目录

refer to link

offical link

code

/**
 * Calls the given block on a nullable variable.
Inside run, the object's members are accessed without its name.
run returns the length of the given String if it's not null
 */
fun main() {
    fun getNullableLength(ns: String?): Int? {
        println("for \"$ns\":")
        val value = ns?.run {
            /*
                the run{} function is similar to mean that: object.run(this){ the code block in the run funciton}
             */
            /**
             * Calls the given block on a nullable variable.
            todo:Inside run, the object's members are accessed without its name.
            run{} returns the length of the given String if it's not null
             */
            println("\tis empty? " + isEmpty())                    // 2
            println("\tlength = $length")
            /*test the return value of the run{}*/
//            length
//            "ok"
        }
        return ns?.run {
            println("testing the return directly.")
            length
        }
//        println("the value of the value of the run{} returned:$value")
    }
    getNullableLength(null)
    getNullableLength("")
    val value = getNullableLength("some string with Kotlin")
    print("print the value returned by run{}:$value")
}
posted @ 2024-09-09 13:07  xuchaoxin1375  阅读(9)  评论(0)    收藏  举报  来源