Swift if let statement in Kotlin

2020-01-30 03:38发布

问题:

In Kotlin is there an equivalent to the Swift code below?

if let a = b.val {

}
else {

}

回答1:

You can use the let-function like this:

val a = b?.let {
    // If b is not null.
} ?: run {
    // If b is null.
}

Note that you need to call the run function only if you need a block of code. You can remove the run-block if you only have a oneliner after the elvis-operator (?:).

Be aware that the run block will be evaluated either if b is null, or if the let-block evaluates to null.

Because of this, you usually want just an if expression.

val a = if (b == null) {
    // ...
} else {
    // ...
}

In this case, the else-block will only be evaluated if b is null.



回答2:

Let's first ensure we understand the semantics of the provided Swift idiom:

if let a = <expr> {
     // then-block
}
else {
     // else-block
}

It means this: "if the <expr> results in a non-nil optional, enter the then-block with the symbol a bound to the unwrapped value. Otherwise enter the else block.

Especially note that a is bound only within the then-block. In Kotlin you can easily get this by calling

<expr>?.also { a ->
    // then-block
}

and you can add an else-block like this:

<expr>?.also { a ->
    // then-block
} ?: run {
    // else-block
}

This results in the same semantics as the Swift idiom.



回答3:

My answer is totally a copy cat from the others. However, I cannot understand their expression easily. So I guess it would be nice to provide an more understandable answer.

In swift:

if let a = b.val {
  //use "a" as unwrapped
}
else {

}

In Kotlin:

b.val?.let{a -> 
  //use "a" as unwrapped
} ?: run{
  //else case
}


回答4:

Here's how to only execute code when name is not null:

var name: String? = null
name?.let { nameUnwrapp ->
    println(nameUnwrapp)  // not printed because name was null
}
name = "Alex"
name?.let { nameUnwrapp ->
    println(nameUnwrapp)  // printed "Alex"
}


回答5:

Unlike Swift, Its not necessary to unwrap the optional before using it in Kotlin. We could just check if the value is non null and the compiler tracks the information about the check you performed and allows to use it as unwrapped.

In Swift:

if let a = b.val {
  //use "a" as unwrapped
} else {

}

In Kotlin:

if b.val != null {
  //use "b.val" as unwrapped
} else {

}

Refer Documentation: (null-safety) for more such use cases



回答6:

if let statement.

Swift's Optional Binding (so called if-let statement) is used to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable. So, an Optional Binding for the if-let statement is as follows:

Swift's if-let statement:

let b: Int? = 50

if let a = b {
    print("Good news!")
} else {
    print("Equal to 'nil' or not set")
}

/*  RESULT: Good news!  */

In Kotlin, like in Swift, to avoid crashes caused by trying to access a null value when it’s not expected, a specific syntax (like b.let { } in second example) is provided for properly unwrapping nullable types:

Kotlin equivalent 1 of Swift's if-let statement:

val b: Int? = null
val a = b

if (a != null) { 
    println("Good news!")
} else { 
    println("Equal to 'null' or not set")
}

/*  RESULT: Equal to 'null' or not set  */

Kotlin’s let function, when used in combination with the safe-call operator ?:, provides a concise way to handle nullable expressions.

Kotlin equivalent 2 (Inline let function and Elvis Operator) of Swift's if-let statement:

val b: Int? = null

val a = b.let { nonNullable -> nonNullable } ?: "Equal to 'null' or not set"
println(a)

/*  RESULT: Equal to 'null' or not set  */

guard let statement.

guard-let statement in Swift is simple and powerful. It checks for some condition and if it evaluates to be false, then the else statement executes which normally will exit a method.

Let's explore a Swift's guard-let statement:

let b: Int? = nil

func testIt() {
    guard let a = b else {
        print("Equal to 'nil' or not set")
        return
    }
    print("Good news!")
}
testIt()

/*  RESULT: Equal to 'nil' or not set  */

Kotlin's similar effect of Swift's guard-let statement:

Unlike Swift, in Kotlin, there is no guard statement at all. However, you can use the Elvis Operator?: for getting a similar effect.

val b: Int? = 50

fun testIt() {
    val a = b ?: return println("Equal to 'null' or not set")
    return println("Good news!")
}
testIt()

/*  RESULT: Good news!  */

Hope this helps.



回答7:

Here's my variant, limited to the very common "if not null" case.

First of all, define this somewhere:

inline fun <T> ifNotNull(obj: T?, block: (T) -> Unit) {
    if (obj != null) {
        block(obj)
    }
}

It should probably be internal, to avoid conflicts.

Now, convert this Swift code:

if let item = obj.item {
    doSomething(item)
}

To this Kotlin code:

ifNotNull(obj.item) { item -> 
    doSomething(item)
}

Note that as always with blocks in Kotlin, you can drop the argument and use it:

ifNotNull(obj.item) {
    doSomething(it)
}

But if the block is more than 1-2 lines, it's probably best to be explicit.

This is as similar to Swift as I could find.



回答8:

There is a similar way in kotlin to achieve Swift's style if-let

if (val a = b) {
    a.doFirst()
    a.doSecond()
}

You can also assigned multiple nullable values

if (val name = nullableName, val age = nullableAge) {
    doSomething(name, age)
}

This kind of approach will be more suitable if the nullable values is used for more than 1 times. In my opinion, it helps from the performance aspect because the nullable value will be checked only once.

source: Kotlin Discussion



标签: kotlin