Kotlin
Summary - Sealed class
Combine sealed class, when and data class to write a result model.
Summary - Sealed class
In this group you learned the sealed class definition, exhaustive branching with when, data class subtypes, state management and the Result pattern.
Now you will combine them: a sealed class, data class subtypes and a when function to write a result model. This structure is the foundation of safe type hierarchies in Kotlin. Later, object declarations and extension functions will build on it.
Exercise Combine the ApiResult model
Define a sealed class named ApiResult. Add two data class subtypes: Success(val data: String) and Failure(val error: String), both inheriting from ApiResult(). Write a function process(r: ApiResult): String whose body uses when (r) to return "data: ${r.data}" for Success and "issue: ${r.error}" for Failure. In main create a Success("record") instance and print the result of process.
Show example solution
sealed class ApiResult
data class Success(val data: String) : ApiResult()
data class Failure(val error: String) : ApiResult()
fun process(r: ApiResult): String {
return when (r) {
is Success -> "data: ${r.data}"
is Failure -> "issue: ${r.error}"
}
}
fun main() {
val r: ApiResult = Success("record")
println(process(r))
}
What comes next?
You learned sealed classes. In the next groups you will explore singletons with object, adding methods to existing types with extension functions and class-level members with companion object.
This group combined sealed class, when, data class subtypes, state management and the Result pattern. These are the core of safe type hierarchies in Kotlin. Next you will see code organization with singletons and extensions.
Exercise The Event sealed class and the handle function
Define a sealed class named Event. Add three subtypes: data class Click(val x: Int, val y: Int) : Event(), data class Key(val key: String) : Event() and object Hover : Event(). Write a function handle(e: Event): String whose body uses when (e) to return "click: ${e.x}, ${e.y}" for Click, "key: ${e.key}" for Key and "hover" for Hover. In main create a Click(10, 20) instance and print the result of handle.
Show example solution
sealed class Event
data class Click(val x: Int, val y: Int) : Event()
data class Key(val key: String) : Event()
object Hover : Event()
fun handle(e: Event): String {
return when (e) {
is Click -> "click: ${e.x}, ${e.y}"
is Key -> "key: ${e.key}"
Hover -> "hover"
}
}
fun main() {
val e: Event = Click(10, 20)
println(handle(e))
}
Test yourself
-
If you add a new subtype to a sealed class, what happens?
- Existing when expressions automatically add the new branch
- The new subtype is deleted
- The sealed class is no longer sealed
- All when expressions give a compile error and force you to add the new branch
Show answer
All when expressions give a compile error and force you to add the new branch — All when expressions give a compile error and force you to add the new branch.
-
Complete the when branch
fun process(r: ApiResult): String {
return when (r) {
____ -> "data: ${r.data}"
is Failure -> "issue: ${r.error}"
}
}
Show answer
fun process(r: ApiResult): String {
return when (r) {
is Success -> "data: ${r.data}"
is Failure -> "issue: ${r.error}"
}
}
Next lesson: object