gpt4 book ai didi

kotlin 避免 `let` 在 `let` 内部深度嵌套

转载 作者:行者123 更新时间:2023-12-03 08:49:12 36 4
gpt4 key购买 nike

有没有办法避免接下来的构造?一种在不向代码添加意图的情况下检查 null 的方法?我的意思是像 if (variableOne == null) return 但采用酷炫的 koltin 风格?

variableOne?.let{
// ....

variableTwo?.let {
// ...

variableThree?.let {
// ...

}
}
}

最佳答案

使用局部变量和智能转换。

val lVariable1 = variable1
val lVariable2 = variable2
val lVariable3 = variable3
if (lVariable1 != null &&
lVariable2 != null &&
lVariable3 != null) {
// Use local variables here
}

关于kotlin 避免 `let` 在 `let` 内部深度嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59966834/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com