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!!")
What is the Actual use of let
?.Is that more memory efficient or more readable?
let is often used for executing a code block only with non-null values. To perform actions on a non-null object, use the safe call operator ?. on it and call let with the actions in its lambda. Another case for using let is introducing local variables with a limited scope for improving code readability.
In Kotlin, if the last statement in a “let” block is a non-assignment statement, it is by default a return statement. For example: private fun performLetOperation() { val person = Person().let { "The name of the Person is: ${it.name}" } print(person) } output: The name of the Person is: Abcd.
Kotlin does not have a new keyword. To create a class instance, call the constructor just like a regular function. We saw that in the screenshot above. Kotlin has single inheritance from a named superclass, and all Kotlin classes have a default superclass Any , which is not the same as the Java base class java.
An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.
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.
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.
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.
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.
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.
Edit: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With