ChatGPT解决这个技术问题 Extra ChatGPT

在 Kotlin 中进行空检查的最佳方法?

我应该使用双 = 还是三重 =

if(a === null)  {
//do something
}

或者

if(a == null)  {
//do something
}

同样对于“不等于”:

if(a !== null)  {
//do something
}

或者

if(a != null)  {
//do something
}
看看链接:- kotlinlang.org/docs/reference/null-safety.html ........在 Kotlin Docs 中很容易

B
Benito Bertoli

结构等式 a == b 被转换为

a?.equals(b) ?: (b === null)

因此,当与 null 比较时,结构等式 a == null 被转换为引用等式 a === null

根据 docs,优化代码没有意义,因此您可以使用 a == nulla != null


注意,如果变量是可变属性,您将无法在 if 语句中将其智能转换为不可为空的类型(因为该值可能已被另一个线程修改)并且您必须将安全调用运算符与 { 5} 代替。

安全电话接线员 ?.

a?.let {
   // not null do something
   println(it)
   println("not null")
}

您可以将它与 Elvis 运算符结合使用。

猫王接线员?: (我猜是因为询问标记看起来像猫王的头发)

a ?: println("null")

如果你想运行一段代码

a ?: run {
    println("null")
    println("The King has left the building")
}

将两者结合

a?.let {
   println("not null")
   println("Wop-bop-a-loom-a-boom-bam-boom")
} ?: run {
    println("null")
    println("When things go null, don't go with them")
}

为什么不使用 if 进行空检查? a?.let{} ?: run{} 仅在极少数情况下适用,否则不符合习惯用法
@voddan 我并不是建议不要将 if 用于 null 检查,而是列出其他可行的选项。虽然我不确定 run 是否有某种性能损失。我会更新我的答案以使其更清楚。
@voddan 如果 avar,则使用 a?.let{} ?: run{} 保证它将在整个范围内正确绑定在 let 中。如果 aval,则没有区别。
@madeinqc 如果 a 是 val,那么使用 let 是不同的,这很糟糕。我发现这篇文章非常善于解释它 - Kotlin: Don’t just use LET for null check
@voddan 我是 Kotlin 的新手。您能否解释或提供一个 URL,说明这不是惯用的?
L
Levon Petrosyan

Kotlin 处理 null 的方式

安全访问操作

val dialog : Dialog? = Dialog()
dialog?.dismiss()  // if the dialog will be null,the dismiss call will be omitted

让函数

user?.let {
  //Work with non-null user
  handleNonNullUser(user)
}

提前退出

fun handleUser(user : User?) {
  user ?: return //exit the function if user is null
  //Now the compiler knows user is non-null
}

不可变的阴影

var user : User? = null

fun handleUser() {
  val user = user ?: return //Return if null, otherwise create immutable shadow
  //Work with a local, non-null variable named user
}

默认值

fun getUserName(): String {
 //If our nullable reference is not null, use it, otherwise use non-null value 
 return userName ?: "Anonymous"
}

使用 val 而不是 var

val 是只读的,var 是可变的。建议尽可能多地使用只读属性,它们是线程安全的。

使用延迟初始化

有时你不能使用不可变的属性。例如,当在 onCreate() 调用中初始化某些属性时,它会在 Android 上发生。对于这些情况,Kotlin 有一个名为 lateinit 的语言功能。

private lateinit var mAdapter: RecyclerAdapter<Transaction>

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   mAdapter = RecyclerAdapter(R.layout.item_transaction)
}

fun updateTransactions() {
   mAdapter.notifyDataSetChanged()
}

我将最后一个称为“默认值”(不是 elvis),因为其中 3/4 使用的是 elvis。
@AjahnCharles 是有道理的))
这是垃圾,任何现代语言都可以比这更好地处理选项。对于程序员来说,这更像是一件苦差事而不是好处。
M
Michael

两种方法都生成相同的字节码,因此您可以选择您喜欢的任何内容。


如果我理解正确,那么他要求在 Kotlin 中检查 null 的最佳方法,而不是哪种方法生成最佳字节码。@BenitoBertoli 答案看起来很有希望,它减少了样板代码
B
BingLi224

除了@Benito Bertoli,

该组合实际上与 if-else 不同

"test" ?. let {
    println ( "1. it=$it" )
} ?: let {
    println ( "2. it is null!" )
}

结果是:

1. it=test

但如果:

"test" ?. let {
    println ( "1. it=$it" )
    null // finally returns null
} ?: let {
    println ( "2. it is null!" )
}

结果是:

1. it=test
2. it is null!

另外,如果先使用 elvis:

null ?: let {
    println ( "1. it is null!" )
} ?. let {
    println ( "2. it=$it" )
}

结果是:

1. it is null!
2. it=kotlin.Unit

V
Vlad

检查有用的方法,它可能很有用:

/**
 * Performs [R] when [T] is not null. Block [R] will have context of [T]
 */
inline fun <T : Any, R> ifNotNull(input: T?, callback: (T) -> R): R? {
    return input?.let(callback)
}

/**
 * Checking if [T] is not `null` and if its function completes or satisfies to some condition.
 */
inline fun <T: Any> T?.isNotNullAndSatisfies(check: T.() -> Boolean?): Boolean{
    return ifNotNull(this) { it.run(check) } ?: false
}

以下是如何使用这些功能的可能示例:

var s: String? = null

// ...

if (s.isNotNullAndSatisfies{ isEmpty() }{
   // do something
}