Don't think the title is best wording for this question, I have an interface with two type arguments, and I seem to be able to cast this
as both of the types at the same time, see reproducable example:
interface BaseResult<S : BaseResult<S, E>, E : BaseResult<S, E>> {
@Suppress("UNCHECKED_CAST")
fun isSuccess(): Boolean {
return try {
val a = this as S //ClassCastException expected
val b = this as E
println(a) //com.BaseResultTest$ExampleResult$Error@17d10166
println(b) //com.BaseResultTest$ExampleResult$Error@17d10166
true
} catch (e: Exception) {
false
}
}
}
Test:
class BaseResultTest {
sealed class ExampleResult: BaseResult<ExampleResult.Success, ExampleResult.Error> {
object Success: ExampleResult()
object Error: ExampleResult()
}
@Test
fun test() {
val error = ExampleResult.Error
assertEquals(error.isSuccess(), false)
}
}
Am I missing something, or is this a bug in the language? Using v1.3.61