Learn when to use the Kotlin scope functions `run`, `also`, and `let` based on context, internal use of `this` or `it`, and desired return values.
---
This video is based on the question https://stackoverflow.com/q/67964079/ asked by the user 'Amit Dash' ( https://stackoverflow.com/u/3556388/ ) and on the answer https://stackoverflow.com/a/67964876/ provided by the user 'Adam Millerchip' ( https://stackoverflow.com/u/1225617/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: When does one use "run" or "also" in place of "let" in Kotlin?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Kotlin Scope Functions: run, also, and let
Kotlin is a modern programming language frequently used for Android app development and server-side applications. Among its many powerful features are scope functions that allow for cleaner, more readable code. However, many developers find themselves confused about when to use certain functions such as let, run, and also. In this post, we will clarify when to use each function, how they differ, and provide guidelines to help you make an informed decision in your coding practices.
The Problem: Choosing Between Scope Functions
When writing Kotlin code, you might find yourself needing to perform operations on an object in a context where it is available. While the let function is commonly used in these scenarios, developers often wonder about alternatives such as run and also. The core question is:
When should I use run or also instead of let?
Once you understand the behavior and use cases for these functions, selecting the right one will become a more intuitive process.
The Solution: Differentiating the Scope Functions
1. Scope Function Basics
Before diving into specifics, let’s clarify what scope functions are: in Kotlin, a scope function is used to execute a block of code within the context of an object. The most commonly used scope functions are:
let: This function is typically used to perform an operation on an object and return a different result. Inside the block, it refers to the object.
run: Similar to let, but it allows the use of this to refer to the object. It returns the lambda result.
also: This function is used for performing additional actions on an object. Within the block, it refers to the object, similar to let, but it returns the original object instead of the lambda result.
2. When to Use Each Function
Using let
Use let when:
You need to operate on a nullable object.
You want to use it to refer to the object directly.
Your goal is to transform or retrieve a different value.
Using run
Use run when:
You want clear access to the object as this, providing more readability.
You need to access properties or methods without repeatedly referencing the object.
The lambda block can return a result that you want to collect.
Using also
Use also when:
You want to perform additional operations that do not change the object itself, like logging or validation.
You prefer to operate with it, similar to let, but want to return the original object afterward.
Your primary goal is to keep your current object intact while executing additional methods.
3. Making Judgement Calls
As with many programming decisions, it ultimately comes down to context and personal judgment. Consider the following factors:
Internal Reference: Choose this vs. it based on which makes your intentions clearer for other developers reading your code.
Return Values: Decide whether you need the result of the operation or if you need to maintain the original object for further operations.
Conclusion
In conclusion, the choice between let, run, and also in Kotlin isn’t definitive; rather, it's a matter of context and readability. By understanding the unique characteristics of each function and their intended use cases, you can enhance your coding efficiency and clarity.
Feel free to experiment with these functions in your Kotlin projects, and soon, selecting the appropriate one will feel second nature. Happy coding!
Информация по комментариям в разработке