// *** before: ***
sealed class OldError {
class NotFound : OldError() // subclasses had to live in the same FILE
class Timeout : OldError()
}
// *** in version 1.5: ***
sealed interface Error
class NotFound(val path: String) : Error
class Timeout(val seconds: Int) : Error
object Unknown : Error
// subclasses may live in different files of the same package and module,
// and a class may implement several sealed interfaces at once
fun describe(e: Error) = when (e) { // no else branch is needed
is NotFound -> "not found: ${e.path}"
is Timeout -> "timeout ${e.seconds}s"
Unknown -> "unknown"
}
fun main() {
println(describe(NotFound("/tmp")))
println(describe(Unknown))
}