Stephan Schroeder
02/08/2021, 11:00 AMKotlin 1.5 removes both constraints: you can now make an interface sealed. The subclasses (both to sealed classes and sealed interfaces) should be located in the same compilation unit and in the same package as the super class, but they can now be located in different files.
Now my question is, can you still extend a sealed interface in a TestCase (within the same module)? Do the tests within a Gradle test
-folder belong to the same sourceSet (and therefore compilation unit?) as the sources under src
?spand
02/08/2021, 11:55 AMEugen Martynov
02/08/2021, 12:37 PMMarc Knaup
02/08/2021, 12:57 PMwhen
expressions in the main source set would no longer be exhaustive because they don't know about tests.Stephan Schroeder
02/08/2021, 2:53 PMMarc Knaup
02/08/2021, 2:54 PMwhen
statements do if they encounter an unexpected implementation? That defeats the purpose of sealed
.
Instead, you should mock each interface
that extends your sealed interface
.Stephan Schroeder
02/08/2021, 2:55 PMMarc Knaup
02/08/2021, 2:55 PMA
and B
implement `S`” but the test says “okay, here’s unrelated `C`“. How would your library be able to handle that?
when (s) {
is A ->
is B ->
} // crash?
Marc Knaup
02/08/2021, 2:57 PMsealed
in Kotlin.
You may be able to do so with Java or maybe by suppressing the error in Kotlin. But both would be a hack.Marc Knaup
02/08/2021, 2:57 PMwhen
. It also affects casting, smart casts and probably other functionality.