Explain the difference between Kotlin's let, run, with, apply, and also

WHAT IT TESTS: Whether you know the two axes of Kotlin scope functions: receiver (this vs it) and return value (context vs lambda result). ANSWER OUTLINE: Group by this (apply, run, with) vs it (let, also), then by return type. RED FLAG: Using them randomly.
WHAT IT TESTS: Your grasp of the two axes distinguishing Kotlin scope functions: receiver reference (this vs it) and return type (context object vs lambda result). ANSWER OUTLINE: Separate this-based functions (apply, run, with) from it-based (let, also), then split by return value: apply and also return the object; let, run, and with return the lambda result. Tie to idioms: apply for configuration, let for nullables, also for side effects, run/with for computation.
Read the original → kotlinlang.org
- #kotlin
- #scope-functions
- #android
- #standard-library
- #beginner
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.