Simon Duchastel
09/24/2025, 4:45 AMwhen statements not trigger a smart cast?
ie:
sealed interface Foo
object Bar: Foo
object Baz: Foo
when (foo) {
Bar -> // typechecked as Foo, cannot use as Bar without a casting
is Baz -> // smart-cast as Baz
}
I thought using the equality syntax would allow smart-casting, but I get a type error in my project if I do this. I tried on Kotlin 2.2.20 (link) and also on an older Kotlin version too (link)Simon Duchastel
09/24/2025, 4:45 AMephemient
09/24/2025, 4:48 AMequals is overridable, so foo.equals(Bar) does not necessarily mean that foo is Barephemient
09/24/2025, 4:49 AMequals, but in general it is possibleSimon Duchastel
09/24/2025, 4:50 AMSimon Duchastel
09/24/2025, 4:50 AMequals, thanks @ephemient!Youssef Shoaib [MOD]
09/24/2025, 4:50 AMdata object at least, but I guess you still could override equals thereSimon Duchastel
09/24/2025, 4:52 AMis check) and it's good that Kotlin is keeping a foot gun away from us by ensuring an overridden equals doesn't blow up the program