我应该在if(data!= null)和data?.let之间使用哪一个? [英] Which one should I use between if (data!=null) and data?.let in kotlin?

查看:391
本文介绍了我应该在if(data!= null)和data?.let之间使用哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码A是Java中的常用用法.

The Code A is common usage in java.

我对Kotin的钥匙完全不了解.在Kotlin中,我应在代码A和代码B之间使用哪一个?谢谢!

I havn't understanded completely the key let of Kotin. Which one should I use between Code A and Code B in kotlin? Thanks!

代码A

if (data!=null){
    initAndBindAdapter()
    mCustomAdapter.setSelectedIndex(data.getIntExtra("index",-1))
}    

代码B

 data?.let {
    initAndBindAdapter()
    mCustomAdapter.setSelectedIndex(it.getIntExtra("index",-1))
 }

还有,如果有趣的do_NoNeed_data_par不需要数据参数,我应该在Kotlin的Code C和Code D之间选择哪个?

And more, which one should I choose between Code C and Code D in kotlin if the fun do_NoNeed_data_par doesn't need data parameter ?

代码C

   if (data!=null){
        do_NoNeed_data_par()        
    }

代码D

   data?.let {
       do_NoNeed_data_par()    
    }

推荐答案

我(个人观点)认为,在可能的地方使用简单的常规null检查是个好主意,尽管?.let方法已在主菜单下列出. Kotlin文档的成语页社区做出贡献)-因此,基本上,这取决于您的个人喜好(更容易理解).

I (personal opinion) think it's a good idea to use simple, regular null checks where you can, although the ?.let method has been listed under the main Kotlin Idioms page of the documentation (which is open for the community to contribute) - so basically, this will be up to your personal preferences of which one is more readable.

更有趣的问题是有什么区别,以及何时可以使用它们:主要区别是let保留变量的值,就像开始调用let时一样.在let块中对it的任何后续使用都将引用相同的值.如果对if使用简单的null检查,则在执行if块的主体时,变量的值可能会更改.

The more interesting question is what are the differences, and when you can use each: the main difference is that let holds on to the value of the variable as it was when the let call on it started, and any subsequent uses of it within the let block will reference that same value. If you use a simple null check with if, your variable's value might be changed while the body of the if block is being executed.

因此,它将无法编译,因为x可以被多个线程访问,并且当您首先为null检查读取它的值时它可能为非null,但是到那时它可能会变为null.您会再次读取println参数-这将是不安全的:

So for example, this won't compile, because x can be accessed by multiple threads, and it might be non-null when you read its value first for the null check, but it might become null by the time you read it again for the println parameter - this would be unsafe:

class Foo {
    var x: Int? = null

    fun useX() {
        if (x != null) {
            println(x + 10) // (...) 'x' is a mutable property that could have been changed by this time
        }
    }
}

但是,let将在相同的情况下工作,因为即使在此期间重新分配了类中的x属性,它也将使用x的初始值在整个执行过程中使用的值:

However, a let will work in the same situation, because it will use whatever the initial value of x had all throughout its execution, even if the x property in the class gets reassigned in the meantime:

class Foo {
    var x: Int? = null

    fun useX() {
        x?.let {
            println(it + 10)
        }
    }
}

您可以想到上面的?.let语句,基本上可以执行此操作,从而创建变量的临时副本:

You can think of the ?.let statement above of basically performing this, creating a temporary copy of your variable:

fun useX() {
    val _x = x
    if (_x != null) {
        println(_x + 10)
    }
}

对该副本进行操作是安全的,因为即使x属性更改了其值,该_x副本对于整个函数仍将为null,或者为非null,并且可以安全使用.

Operating on this copy is safe, because even if the x property changes its value, this _x copy will either stay null for this entire function, or it's non-null and safe to use.

这篇关于我应该在if(data!= null)和data?.let之间使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆