So in the following code I get compilation error that "when must be exhaustive add necessary else":
class Test {
}
fun eval(e: Test): Int =
when(e) {
is Test -> throw IllegalArgumentException()
}
To clarify this is only code aimed to understand Kotlin (newbie here).
So Test
can not be extended by any subclass by default behaviour of Kotlin.
So what are other cases that the when
expects?
Using sealed
also does not work in this case
class Test
Because when needs default value for return, if the elements (e) is not matched anything it should be return default value
other option is change return type as nullable in your function
when
.
for more details refer when-expression
if elements is single no point of use when. your function may be simplified as below