Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)
Sealed Classes and Sealed Interfaces: Algebraic Data Types and pattern matching.
senior LevelCode ExamplePerformanceTypeScript
In Simple Words: Restricts inheritance to subclasses declared within the same package and compilation module:
kotlin
sealed interface NetworkState<out T> {
data object Loading : NetworkState<Nothing>
data class Success<T>(val data: T) : NetworkState<T>
data class Error(val exception: Throwable) : NetworkState<Nothing>
}
- Exhaustive
when: The compiler verifies that all cases are covered. Noelsebranch is needed; if a new subclass is added, the compiler flags all unhandledwhenstatements as errors.