I find this kinda interesting: ```sealed interface...
# getting-started
t
I find this kinda interesting:
Copy code
sealed 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?
k
Try this:
Copy code
sealed 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)
}
t
thanks. in the actual, I went with
Copy code
sealed 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)
m
They cannot smart cast, because equals does not guarantee that they are the same type as Klitos showed
t
ah, makes sense, I did not catch Klitos point. thanks you both