Joffrey
05/08/2021, 10:16 AMexpect sealed class
(coming from Ktor multiplatform client), which used to compile fine (on 1.4.31), but now throws the following error:
when with expect sealed class as subject can not be exhaustive without else branch
I've seen this bug: https://youtrack.jetbrains.com/issue/KT-43875 but it seems to report a problem on 1.4, while my code used to compile fine on 1.4.31Youssef Shoaib [MOD]
05/08/2021, 10:23 AMYoussef Shoaib [MOD]
05/08/2021, 10:26 AMYoussef Shoaib [MOD]
05/08/2021, 10:30 AM// !LANGUAGE: +MultiPlatformProjects
// !DIAGNOSTICS: -UNUSED_VARIABLE
// ISSUE: KT-20306
// MODULE: m1-common
// FILE: common.kt
expect enum class Base {
A, B
}
fun testCommon(base: Base) {
val x = when (base) { // must be an error
Base.A -> 1
Base.B -> 2
}
}
// MODULE: m1-jvm(m1-common)
// FILE: Base.kt
actual enum class Base {
A, B, C
}
fun testPlatformGood(base: Base) {
val x = when (base) { // must be OK
Base.A -> 1
Base.B -> 2
Base.C -> 3
}
}
fun testPlatformBad(base: Base) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (base) { // must be an error
Base.A -> 1
Base.B -> 2
}
}
Joffrey
05/08/2021, 10:33 AMStrum355
05/08/2021, 12:27 PMYoussef Shoaib [MOD]
05/08/2021, 12:41 PMexpect sealed class
can have unexpected inheritors, and so it's only safe to use it exhaustively in platform code not common code. If you need exhaustiveness in common code, then make it a normal sealed class
and make all the inheritors expect
insteadStrum355
05/08/2021, 12:45 PMelizarov
05/09/2021, 9:26 AMJoffrey
05/09/2021, 9:29 AM