Functions.kt
  1. 1sealed interface Load<out T> {
  2. 2 data object Idle : Load<Nothing>
  3. 3 data object Loading : Load<Nothing>
  4. 4 data class Ready<T>(val value: T) : Load<T>
  5. 5 data class Failed(val reason: String) : Load<Nothing>
  6. 6}

Functions.kt

Functions, values, and when

val names a value that does not get reassigned. var is the exception, used when a screen really mutates. Functions declare their return type when the body is a block, and the compiler infers it when the body is an expression.

when replaces a ladder of types and states: a load can be Idle, Loading, Ready, or Failed. Each branch returns the same kind of result, so the screen cannot forget a state.

Data classes give you copy, equals, and a readable toString. A lesson model is a data class. A repository is not.

Danville · educational · 2018AboutDeskFAQOffice