In this tutorial, we’ll be implementing some of the important standard library functions available in Kotlin. The kotlin-stdlib provides us with useful higher order functions implementing idiomatic patterns. We’ll see how they make programming in Kotlin so easier and faster.

The functions that we’re going to discuss below are:

Kotlin let

let takes the object it is invoked upon as the parameter and returns the result of the lambda expression.
Kotlin let is a scoping function wherein the variables declared inside the expression cannot be used outside.

An example demonstrating kotlin let function is given below.

it keyword contains the copy of the property inside let.

The last value from the let is returned as an argument as shown below.


Chaining let functions

As you can see we’ve declared a local variable “i” inside the second let function. Setting the last statement of the let function to i returns the property to the outer property a.


Nesting let

We can set a let expression inside another let expression as shown below.

For nested let, we can’t use it keyword. We need to assign explicit names to it in both the let functions.
Only the outermost let returns the value as shown below.


let for null checks

Additionally, let is useful for checking Nullable properties as shown below.

The code inside the let expression is executed only when the property is not null. Thus let saves us from the if else null checker too!

Kotlin run

Kotlin run is another interesting function. The following example demonstrates its use cases.

Kotlin run expression can change the outer property. Hence in the above code, we’ve redefined it for the local scope.

  • Similar to the let function, the run function also returns the last statement.
  • Unlike let, the run function doesn’t support the it keyword.

let and run

Let’s combine the let and run functions together.

Kotlin also

As the name says, also expressions does some additional processing on the object it was invoked.
Unlike let, it returns the original object instead of any new return data. Hence the return data has always the same type.
Like let, also uses it too.

Kotlin let vs also

Following code snippet shows a great example to differentiate between let and also.

In the above code, we’ve used Data classes.

The also expression returns the data class object whereas the let expression returns nothing (Unit) as we didn’t specify anything explicitly.

Kotlin apply

Kotlin apply is an extension function on a type. It runs on the object reference (also known as receiver) into the expression and returns the object reference on completion.

apply vs also


Note: In apply it isn’t allowed. If the property name of the data class is unique in the function, you can omit this.

We should use also only when we don’t want to shadow this.

Kotlin with

Like apply, with is used to change instance properties without the need to call dot operator over the reference every time.

Again with is similar to apply except for a few differences.

Kotlin apply vs with

  • with runs without an object(receiver) whereas apply needs one.
  • apply runs on the object reference, whereas with just passes it as an argument.
  • The last expression of with function returns a result.

That’s all for Kotlin standard functions to alter variables or modify objects within the function.

By admin

Leave a Reply