thanksforallthefish
02/07/2025, 1:35 PMsealed interface Test {
object NoOp : Test {
fun f() {}
}
}
fun dispatch(test: Test) = when (test) {
Test.NoOp -> test.f() <---- does not compile
}
fun dispatch(test: Test) = when (test) {
is Test.NoOp -> test.f() <---- compiles
}
I assume there is some valid reason for which smart casting cannot work in such a scenario, anyone keen to explain me?Klitos Kyriacou
02/07/2025, 1:59 PMsealed interface Test {
object NoOp : Test {
fun f() {
println("f()")
}
}
}
object Other : Test {
override fun equals(other: Any?): Boolean {
return true
}
}
fun dispatch(test: Test) = when (test) {
Test.NoOp -> if (test is Test.NoOp) test.f() else println(test::class)
Other -> TODO()
}
fun main() {
dispatch(Other)
}
thanksforallthefish
02/07/2025, 2:11 PMsealed interface Test {
object NoOp : Test {
fun f() {}
}
}
fun dispatch(test: Test) = when (test) {
Test.NoOp -> Test.NoOp.f()
}
since NoOp
is an object, so it's constant 🤷
I am just curious with the under the hood (but ofc, also a better solution is always welcome)Michael Krussel
02/07/2025, 3:28 PMthanksforallthefish
02/07/2025, 3:50 PM