KOTLIN / CATATAN</>↗Kotlin01 Jul 2026
You can avoid “nullable everywhere” in complex data models by making missing/invalid/loading/error states explicit in the type system instead of representing them with null. In Kotlin, the usual tools are: Sealed classes/interfaces for domain states and variants. Result<T>-style wrappers for success/failure. Non-nullable data classes for valid, fully constructed domain objects. Mapping layers from nullable external […]
KOTLIN / CATATAN</>↗Kotlin01 Jul 2026
In Kotlin, you can eliminate redundant null checks by letting the compiler smart cast a nullable value after you prove it is not null. Basic smart cast fun printLength(text: String?) { if (text != null) { println(text.length) } } Inside the if block, Kotlin knows text cannot be null, so it treats it as a […]
KOTLIN / CATATAN</>↗Kotlin01 Jul 2026
You combine scope functions with Kotlin DSLs by using each scope function for a clear role: apply {} to configure DSL objects also {} to log, validate, or attach side effects run {} to produce a final value let {} to transform intermediate values with {} to operate on an existing DSL context The most […]
KOTLIN / CATATAN</>↗Kotlin30 Jun 2026
Design Kotlin APIs so that nullability communicates meaning, not uncertainty. A caller should be able to understand from the type alone whether a value is required, optional, absent, unknown, invalid, or failed. 1. Prefer non-null types by default Use nullable types only when null is a valid part of the API contract. fun sendEmail(address: String, […]
KOTLIN / CATATAN</>↗Kotlin29 Jun 2026
Kotlin’s null safety works especially well with collections and maps because Kotlin lets you distinguish between: a nullable collection: List<String>? a collection containing nullable values: List<String?> both: List<String?>? The same idea applies to maps. 1. Nullable collection vs nullable elements val names: List<String>? = null val nicknames: List<String?> = listOf("Ana", null, "Sam") val maybeNicknames: List<String?>? […]
KOTLIN / CATATAN</>↗Kotlin29 Jun 2026
In Kotlin, takeIf and takeUnless are scope-style functions used to keep or discard a value based on a condition. takeIf takeIf returns the object itself if the predicate is true; otherwise it returns null. val result = value.takeIf { condition } Equivalent to: val result = if (condition) value else null Example val number = […]