ghosalmartin
04/21/2022, 7:40 AMNon exhaustive 'when' statements on sealed class/interface will be prohibited in 1.7, add 'else' branch
Does that mean using a sealed class/interface will no longer be exhaustive in a when
. I thought that was a feature of ``sealed`?ephemient
04/21/2022, 8:17 AMelse
Roukanken
04/21/2022, 8:18 AMwhen
statements, you don't need else - there is an implicit "else -> do nothing" in statements (when's without return value)
in 1.7 this will no longer be case, and you'll need to add the else clause even in statementsephemient
04/21/2022, 8:20 AMRoukanken
04/21/2022, 8:20 AMghosalmartin
04/21/2022, 9:03 AMwhen
to a variable and the switching type is a sealedRoukanken
04/21/2022, 9:05 AMephemient
04/21/2022, 9:19 AMwhen
must be exhaustive, always - either by having full branch coverage or by else
, regardless of the type. if the value is not used, previously Kotlin did not enforce exhaustive when
, but for enums and sealed types it will.Richard Gomez
04/21/2022, 11:25 PMwhen
block and a `sealed interface.`It forced me to add an else
statement despite having all the cases defined.ghosalmartin
04/22/2022, 8:14 AMsealed interface House {
data class Semidetached(val sharedWall: String): House
data class Detached(val noSharedWall: String): House
}
fun test(){
val myHouse: House = House.Detached("")
when(myHouse){
is House.Detached -> myHouse.noSharedWall
is House.Semidetached -> myHouse.sharedWall
}
}
ephemient
04/22/2022, 11:00 AMRichard Gomez
04/22/2022, 12:02 PMIDE or command-line compiler? there's a number of IJ plugin bugs around this, such asIn my case it was the IDE. Good to know.