Kotlin scope function comparison: let, also, run, apply, with

Article Reference
Article Reference

1. Comparison of scope functions

insert image description here

let: return the last line
also: similar to let, but return the context
with: this implicit access, return the last line
run: a combination of let and with, return the last line
apply: similar to run, but return the context

Note:
Try to avoid excessive use of scope functions, which will reduce the readability of the code;
try to avoid nested use of scope functions;
pay attention to distinguishing the context object (it or this) when chaining calls

2. Usage scenarios and features

2.1, let() function

  1. Usage scenario : the operation of nullable variables, no need to judge null
fun testLet(p: People?) {
    
    
    //返回值为最后一行
    p?.let {
    
    
        it.name = "leon"
        it.sex = "man"
        it.age = "26"
        it.displayInfo() //name: leon,  sex: man,  age: 26
    }
    p!!.displayInfo() //name: leon,  sex: man,  age: 26
}
  1. Features :
  • The return value is the last row

2.2, also () function

  1. Usage scenario : Multiple extension function chain calls
fun testAlso(p: People?) {
    
    
    //返回值上下文
    p?.also {
    
    
        it.name = "leon"
        it.sex = "man"
        it.age = "26"
        it.displayInfo() //name: leon,  sex: man,  age: 26
    }?.displayInfo() //name: leon,  sex: man,  age: 26
    p!!.displayInfo() //name: leon,  sex: man,  age: 26
}
  1. Features :
  • similar to let
  • Exception handling can be shorted
  • The return value is itself, which can be processed in chain calls

2.3. with() function

  1. Usage scenario : When calling multiple methods of the same class, if you are lazy and don’t even want to write it, you can save the repetition of class names and just call the methods of the class directly
fun testWith(p: People?) {
    
    
    //返回最后一行
    //with无法判空
    with(p) {
    
    
        this?.name = "leon"
        this?.sex = "man"
        this?.age = "26"
        this?.displayInfo()
    }
    
    //用let判空
    p?.let {
    
    
        with(it) {
    
    
            name = "leon"
            sex = "man"
            age = "26"
            displayInfo()
        }
    }
}
  1. features
  • Objects need to be passed, use this or omit to represent the object itself
  • No short exception handling
  • The return value is the last row

2.4 run() function

  1. Usage scenarios : all scenarios used by let and with. (run combines the characteristics of both let and with)
fun testRun(p: People?) {
    
    
    //返回最后一行
    //【方式一 with】: run(T)
    run {
    
    
        p?.name = "leon"
        p?.sex = "man"
        p?.age = "26"
        p?.displayInfo()
    }


    //【方式二 let】: T.run{}
    p?.run {
    
    
        name = "leon"
        sex = "man"
        age = "26"
        displayInfo()
    }
}
  1. Features :
  • The run function is a combination of let and with, using this or omitting to represent the object itself
  • Exception handling can be shorted
  • The return value is the last row

2.5, apply () function

  1. Usage scenario : need to do some operations on the object itself, and then return the object itself
  • Object instance initialization requires initialization and assignment of object properties
  • When dynamically inflating an XML View, it is also necessary to bind data to the View.
fun testApply(p: People?) {
    
    
    //返回上下文
    p?.apply {
    
    
        name = "leon"
        sex = "man"
        age = "26"
        displayInfo()
    }?.displayInfo()
}
  1. Features :
  • Similar to run, it is also this or omitted to represent itself
  • Exception handling can be shorted
  • The return value is itself, which can be processed in chain calls

Guess you like

Origin blog.csdn.net/weixin_41733225/article/details/130029474