Summary - Sealed class
- Kotlin course
- Intermediate
- Lesson 29/116
- 5 min
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.
Practice 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.
Starter code
// Define the ApiResult sealed class
// data class Success and Failure subtypes
// process function: branch with when
// instantiate Success in main, print processSample 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))
}Expected output
data: record
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.
Practice 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.
Starter code
// Define the Event sealed class
// data class Click, data class Key, object Hover subtypes
// handle function: branch with when
// instantiate Click in main, print handleSample 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))
}Expected output
click: 10, 20
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
Explanation: All when expressions give a compile error and force you to add the new branch.
2 Complete the when branch
fun process(r: ApiResult): String {
return when (r) {
____ -> "data: ${r.data}"
is Failure -> "issue: ${r.error}"
}
}
fun process(r: ApiResult): String {
return when (r) {
is Success -> "data: ${r.data}"
is Failure -> "issue: ${r.error}"
}
}
Explanation: is checks the type and enables smart cast.