Using Sealed Interfaces for Enhanced Type Safety

Sealed interfaces are a powerful feature in Kotlin that allow you to define a restricted hierarchy of types. By using sealed interfaces, you can ensure that all implementations are known at compile time, providing better type safety and exhaustive when expressions. This is particularly useful in state management or when dealing with multiple data types in a single API.

interface ApiResponse

sealed interface Success : ApiResponse {
    data class Data(val content: String) : Success
}

sealed interface Error : ApiResponse {
    data class NotFound(val message: String) : Error
    data class ServerError(val message: String) : Error
}

fun handleResponse(response: ApiResponse) {
    when (response) {
        is Success.Data -> println("Data received: ${response.content}")
        is Error.NotFound -> println("Error: ${response.message}")
        is Error.ServerError -> println("Error: ${response.message}")
    }
}