Mateusz Konieczny
09/05/2022, 4:17 PMelse
branch even if not needed in Kotlin 1.7?
See say https://github.com/streetcomplete/StreetComplete/commit/1972a7139bff92e894474d1d3d1969b83da2d375
For example
private fun wat(element: StackUnderflow) {
when (element) {
is Unicorn -> {}
is Multicorn -> {}
else -> {}
}
}
requires useless else
where
sealed class StackUnderflow
sealed class Unicorn(val surface: Surface) : StackUnderflow()
sealed class Multicorn : StackUnderflow()
sealed class Tricorn(val surface: Surface) : Multicorn()
sealed class Doublecorn(val surface: Surface) : Multicorn()
is imported from another file.Joffrey
09/05/2022, 4:22 PMwhen
statements became exhaustive like when
expressions. In 1.6 it was just a warning, but it was raised to error in 1.7. See the notes of 1.6 release.
Also the original issue for more information:
https://youtrack.jetbrains.com/issue/KT-47709Joffrey
09/05/2022, 4:25 PMwhen
statements listing all cases, but then someone adds a new possibility (new enum value or sealed class subtype), and suddenly such when
statements would silently become no-ops. Now, it will fail compilation unless the dev actually adds a branch for the new value, even if just to explicitly do nothing.Joffrey
09/05/2022, 4:31 PMsealed
keyword on the leaf classes seems to solve the issue:
https://pl.kotl.in/xinFLOmDq
Maybe a bug?Mateusz Konieczny
09/05/2022, 5:03 PMIt's not "unnecessary" per se,in general I agree but in this case it was triggered and demanding
else
branch in several places where all currently existing solutions were covered!
So future addition will swallowed by useless else
branch demanded nowMateusz Konieczny
09/05/2022, 5:07 PMsealed class StackUnderflow
sealed class Unicorn(val surface: Surface) : StackUnderflow()
sealed class Multicorn : StackUnderflow()
class Tricorn(val surface: Surface) : Multicorn()
class Doublecorn(val surface: Surface) : Multicorn()
is not triggering itJoffrey
09/05/2022, 5:09 PMMateusz Konieczny
09/05/2022, 5:11 PMfun main() {
println("aaa")
}
private fun wat(element: StackUnderflow) {
when (element) {
is Unicorn -> {}
is Multicorn -> {}
}
}
class Surface
sealed class StackUnderflow
sealed class FooBar: StackUnderflow()
sealed class Unicorn(val surface: Surface) : StackUnderflow()
sealed class Multicorn : StackUnderflow()
class Tricorn(val surface: Surface) : Multicorn()
class Doublecorn(val surface: Surface) : Multicorn()
worksMateusz Konieczny
09/05/2022, 5:11 PMMateusz Konieczny
09/05/2022, 5:12 PMMateusz Konieczny
09/05/2022, 5:15 PMdespite that FooBar is not handledmaybe that is because
sealed
class cannot be instantiated?Mateusz Konieczny
09/05/2022, 5:16 PMJoffrey
09/05/2022, 5:17 PMmaybe that is becauseYeah I was about to say that. It seems to behave fine with realistic hierarchies. If you have a sealed class without children, it cannot be instantiated so it's ok to skip it.class cannot be instantiated?sealed
Joffrey
09/05/2022, 5:18 PMJoffrey
09/05/2022, 5:19 PMMateusz Konieczny
09/05/2022, 5:20 PMsealed class StackUnderflow
sealed class FooBar: StackUnderflow()
data class Unicorn(val surface: String) : FooBar()
data class Multicorn(val surface: String, val note: String) : FooBar()
class Tricorn : FooBar()
data class Doublecorn(val surface: String) : StackUnderflow()
private fun wat(element: StackUnderflow) {
when (element) {
is Unicorn -> {}
is Multicorn -> {}
is Tricorn -> {}
is Doublecorn -> {}
}
}
Mateusz Konieczny
09/05/2022, 5:20 PMMateusz Konieczny
09/05/2022, 5:22 PMJoffrey
09/05/2022, 5:26 PMwhen
statement in a different module than the sealed class hierarchy? I remember seeing similar issues in a big multi-module projectMateusz Konieczny
09/05/2022, 5:30 PMMateusz Konieczny
09/05/2022, 5:31 PMMateusz Konieczny
09/05/2022, 5:31 PMJoffrey
09/05/2022, 5:33 PMJoffrey
09/05/2022, 5:34 PM