Last active
July 1, 2024 18:51
-
-
Save ImaginativeShohag/5a179ccd6596bfcc2d73bde5b9ac7eb6 to your computer and use it in GitHub Desktop.
Difference between with, run, let, also and apply in Kotlin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Difference between with, run, let, also and apply in Kotlin. | |
* | |
* Official doc: https://kotlinlang.org/docs/scope-functions.html#functions | |
* | |
* Awesome blog posts: | |
* - https://www.journaldev.com/19467/kotlin-let-run-also-apply-with | |
* - https://medium.com/@fatihcoskun/kotlin-scoping-functions-apply-vs-with-let-also-run-816e4efb75f5 | |
* - https://medium.com/@elye.project/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84 | |
*/ | |
import kotlin.random.* | |
fun main() { | |
var name = "Shohag" | |
// with() | |
// -------------------- | |
// Inside: Change instance properties without the need to call dot operator. | |
// Context: The context object is available as a receiver `(this)`. | |
// Return: The return value is the lambda result. | |
var with_return_result = with(name) { | |
// ... | |
// name.length | |
// or, just: | |
length // <-- returned result of this | |
} | |
println(with_return_result) | |
// run() | |
// -------------------- | |
// Inside: change instance properties without the need to call dot operator. | |
// Context: The context object is available as a receiver `(this)`. | |
// Return: The return value is the lambda result. | |
var run_return_result = name.run { | |
// ... | |
// name.length | |
// or, just: | |
length // <-- returned result of this | |
} | |
println(run_return_result) | |
// let() | |
// -------------------- | |
// Inside: Use `it` or `anyCustomName` | |
// Context: The context object is available as an argument `(it)`. | |
// Return: The return value is the lambda result. | |
var let_return_result = name.let { anyCustomName -> | |
// ... | |
anyCustomName.length // <-- returned result of this | |
} | |
println(let_return_result) | |
// also() | |
// -------------------- | |
// Inside: Use `it` or `anyCustomName`. | |
// Context: The context object is available as an argument `(it)`. | |
// Return: The return value is the object itself. | |
var also_return_result = name.also { anyCustomName -> | |
// ... | |
println("Name length: ${anyCustomName.length}") | |
} | |
println(also_return_result) | |
// apply() | |
// -------------------- | |
// Inside: Use `this`, `this` can be ommited. | |
// Context: The context object is available as a receiver `(this)`. | |
// Return: The return value is the object itself. | |
var apply_return_result = name.apply { | |
// ... | |
println("Name is: ${this}") | |
println("Name length: ${length}") | |
} | |
println(apply_return_result) | |
// takeIf() and takeUnless() | |
// -------------------- | |
// `takeIf` will take the object if the predicate is `true` and `takeUnless` will not take the object if the predicate `true`. | |
// | |
// Context: The context object is available as a lambda argument `(it)`. | |
val number = Random.nextInt(100) | |
val evenOrNull = number.takeIf { it % 2 == 0 } | |
val oddOrNull = number.takeUnless { it % 2 == 0 } | |
println("even: $evenOrNull, odd: $oddOrNull") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment