The Kotlin standard library contains several functions whose sole purpose is to execute a block of code within the context of an object. In the scope, you can access the object without its name. Such functions are called scope functions. There are five of them: let, run, with, apply, and also.
Basically, these functions all perform the same action: execute a block of code on an object. What's different is how this object becomes available inside the block and what the result of the whole expression is. Here's a typical example of how to use a scope function:
Scope functions don't introduce any new technical capabilities, but they can make your code more concise and readable.
Here's a typical example of how to use a scope function:
If you write the same without let, you'll have to introduce a new variable and repeat its name whenever you use it.
Due to the many similarities between scope functions, choosing the right one for your use case can be tricky. The choice mainly depends on your intent and the consistency of use in your project. Below, we provide detailed descriptions of the differences between scope functions and their conventions.
Here is a short guide for choosing scope functions depending on the intended purpose:
Executing a lambda on non-nullable objects: let
Introducing an expression as a variable in local scope: let
Object configuration: apply
Object configuration and computing the result: run
Running statements where an expression is required: non-extension run
Additional effects: also
Grouping function calls on an object: with
Scope functions differ by the result they return:
apply and also return the context object.
let, run, and with return the lambda result.
You should consider carefully what return value you want based on what you want to do next in your code. This helps you to choose the best scope function to use.
Apply-Also & Let-Run-With Examples↗️
We recommend using with for calling functions on the context object when you don't need to use the returned result. In code, with can be read as "with this object, do the following."
let is often used to execute a code block containing 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.
run does the same as with but it is implemented as an extension function. So like let, you can call it on the context object using dot notation.
takeUnless has the opposite logic of takeIf. When called on an object along with a predicate, takeUnless returns null if it satisfies the given predicate. Otherwise, it returns the object.↗️