在Kotlin有一个相当于Swift代码下面?

if let a = b.val {

} else {

}

当前回答

Any的问题是什么?让{}?:运行{}结构是:

它只允许每条语句进行一次非空检查 如果let块返回null,运行块无论如何都会被求值 不可能在switch/when样式中执行多次检查

解决这些问题的一个解决方案是定义如下的函数:

private inline fun <A> ifNotNull(p1: A?, block: (A) -> Unit): Unit? {
    if (p1 != null) {
        return block.invoke(p1)
    }
    return null
}

private inline fun <A, B> ifNotNull(p1: A?, p2: B?, block: (A, B) -> Unit): Unit? {
    if (p1 != null && p2 != null) {
        return block.invoke(p1, p2)
    }
    return null
}

private inline fun <A, B, C> ifNotNull(p1: A?, p2: B?, p3: C?, block: (A, B, C) -> Unit): Unit? {
    if (p1 != null && p2 != null && p3 != null) {
        return block.invoke(p1, p2, p3)
    }
    return null
}

这将允许这样的语句:

ifNotNull(a, b) { a, b ->
    // code when a, b are not null
} ?:
ifNotNull(c) { c ->
    // code when a, b are null and c not null
} ?: 
ifNotNull(d, e, f) { d, e, f ->
    // code when a, b, c are null and d, e, f not null
} ?: run {
    // code which should be performed if a, b, c, d, e and f are null
}

唯一需要注意的是,与Swift的if let相比,如果在循环中执行,则不支持continue和break语句。

其他回答

我添加这个答案是为了澄清已接受的答案,因为它太大了,不适合评论。

这里的一般模式是,你可以使用Kotlin中可用的范围函数的任何组合,由Elvis操作符分隔,如下所示:

<nullable>?.<scope function> {
    // code if not null
} :? <scope function> {
    // code if null
}

例如:

val gradedStudent = student?.apply {
    grade = newGrade
} :? with(newGrade) {
    Student().apply { grade = newGrade }
}

与Swift不同的是,在Kotlin中使用它之前,不需要打开可选选项。我们可以检查该值是否为非空,编译器会跟踪您执行的检查信息,并允许将其作为解包装使用。

迅速:

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

}

在芬兰湾的科特林:

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

}

更多这样的用例请参考Documentation:(null-safety)

上面有两个答案,都得到了很多人的接受:

str ?。让{}?:运行{} str ?。还有{}?:run {}

两者似乎都适用于大多数用法,但#1在下面的测试中会失败:

第二条似乎更好。

如果b是一个成员变量,那么这种方法对我来说似乎是最有可读性的:

val b = this.b
if (b == null) {
    return
}
println("non nullable : ${b}")

这也与它在swift中的工作方式一致,在swift中,一个新的局部变量遮蔽了成员变量。

让我们首先确保我们理解了所提供的Swift习语的语义:

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

它的意思是:“如果<expr>结果为非nil可选,则输入then块,并将符号a绑定到未包装的值。否则进入else块。

特别注意,a只在then块内绑定。在Kotlin中,您可以通过调用很容易地得到这个

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

你可以像这样添加一个else-block:

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

这将导致与Swift习惯用法相同的语义。