Kotlin中"let"关键字的目的是什么 [英] What is the purpose of 'let' keyword in Kotlin

查看:492
本文介绍了Kotlin中"let"关键字的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以编写带有或不带有let的代码,如下所示.

We can write the code with or without let as follows.

var str = "Hello World"
str.let { println("$it!!") }

OR

var str = "Hello World"
println("$str!!")

let的实际用途是什么?是提高内存使用效率还是提高可读性?

What is the Actual use of let?.Is that more memory efficient or more readable?

推荐答案

let是Kotlin的

let is one of Kotlin's Scope functions which allow you to execute a code block within the context of an object. In this case the context object is str. There are five of them: let, run, with, apply, and also. Their usages range from but are not exclusive to initialization and mapping.

它们都非常相似,但是在引用上下文对象和返回值方面不同.在let的情况下,上下文对象由it关键字而不是this关键字引用.返回值是从lambda代码块返回的值.其他作用域函数(如apply)将改为返回上下文对象.

They are all very similar but they differ in terms of how the context object is referenced and the value that is returned. In the case of let the context object is referenced by the it keyword as opposed to the this keyword. The return value is whatever is returned from the lambda code block. Other scope functions like apply will return the context object instead.

因为let返回lambda块求值的任何内容,所以它最适合执行某种映射:

Because let returns whatever the lambda block evaluates to, it is most suited to performing a mapping of some kind:

var upperStr = str.let { it.toUpperCase()}

应用是一种更适合您的功能做.

apply is a more suited function for what you are doing.

要回答有关哪个代码更可取的问题,这实际上取决于您使用范围函数的作用.在上述情况下,没有理由使用let.如果使用的是IntelliJ,则会发出警告,指出对let的调用是多余的.这里的可读性是一个优先事项,可能是首选.

To answer your question as to which code is more preferable, it really depends on what you are using the scope function for. In the above case there is no reason to use let. If you are using IntelliJ it will give a warning saying the call to let is redundant. Readability here is a matter of preference, and may be preferred.

当您希望使用safe call运算符?.对对象执行null安全操作时,let函数非常有用.在执行此操作时,仅当对象为let代码块时才执行let代码块不为空.使用let的另一个原因是,如果您需要为该操作引入新变量,但希望将它们限制在let块的范围内.对于所有作用域函数都是如此,因此我重申let最好用于映射操作.

The let function is useful when you wish to perform a null safe operation on an Object by using the the safe call operator ?. When doing this the let code block will only be executed if the object is not null. Another reason to use let is if you need to introduce new variables for the operation but you want to confine them to the scope of the let block. This is true for all scope functions, so I reiterate that let is best used for a mapping operation.

let功能应该不会产生任何额外费用.通常,我们希望将lambda/Code-block编译为Function对象,但是Kotlin中的inline函数不是这种情况,对于该函数,编译器将发出与您给出的第二个代码示例相同的代码.有关更多信息,请参见文档.

The let function should incur no additional cost. Normally we would expect the lambda/Code-block to be compiled to a Function object but this is not the case for an inline function in Kotlin for which the compiler will emit code not dissimilar to the second code example you have given. See the documentation for more information.

这篇关于Kotlin中"let"关键字的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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