Kotlin and idiomatic way to write, 'if not null, else…' based around mutable value

前端 未结 5 1675
谎友^
谎友^ 2021-02-01 12:53

Suppose we have this code:

class QuickExample {

    fun function(argument: SomeOtherClass) {
        if (argument.mutableProperty != null ) {
            doSome         


        
5条回答
  •  暖寄归人
    2021-02-01 13:33

    i usually write it like this:

      takeIf{somecondition}?.also{put somecondition is met code}?:run{put your else code here}
    

    note the question mark after takeIf is a MUST. you can use also or apply keyword.

提交回复
热议问题