KOTLIN / CATATAN</>↗Kotlin28 Jun 2026
In Kotlin, scope functions can be chained because each one returns either: the receiver object: also, apply the lambda result: let, run, with The key is understanding what each function returns. Common chaining pattern val result = User("Alice") .also { println("Created user: $it") } .apply { name = name.uppercase() } .let { "User name is […]
KOTLIN / CATATAN</>↗Kotlin28 Jun 2026
Kotlin scope functions all do the same broad thing: they execute a block of code in the context of an object. The main differences are: How you refer to the object: this or it What the function returns: the object itself or the block result Quick decision table Function Object reference Returns Best for let […]
KOTLIN / CATATAN</>↗Kotlin28 Jun 2026
In Kotlin, run, apply, also, and with are scope functions. They help make code cleaner by giving you a temporary scope around an object. The main differences are: Function Object reference Returns Best used for apply this The original object Configuring an object also it The original object Side effects, logging, validation run this Lambda […]
KOTLIN / CATATAN</>↗Kotlin28 Jun 2026
Use ?.let { … }. The safe-call operator ?. makes sure let is called only when the value is not null. Inside the let block, the value is available as a non-null value, usually named it. val name: String? = "Kotlin" name?.let { println("Name is $it") println("Length is ${it.length}") } If name is "Kotlin", the […]
KOTLIN / CATATAN</>↗Kotlin26 Jun 2026
In Kotlin, !! is the not-null assertion operator. It forcefully converts a nullable value like String? into a non-null value like String. val name: String? = getName() val length = name!!.length This tells Kotlin: “Trust me, name is not null.” If name is actually null at runtime, Kotlin throws a NullPointerException. val name: String? = […]
KOTLIN / CATATAN</>↗Kotlin26 Jun 2026
Use Kotlin’s Elvis operator ?: with throw on the right-hand side: val value: String? = getNullableValue() val nonNullValue: String = value ?: throw IllegalArgumentException("value must not be null") Because throw is an expression in Kotlin, it can be used after ?:. Example fun printLength(text: String?) { val nonNullText = text ?: throw IllegalArgumentException("text must not […]