Is smartcasting currently only available in kotlin...
# multiplatform
s
Is smartcasting currently only available in kotlin jvm?
j
Yeah. In any platform it is available.
s
wait, "Yeah" and "In any platform it is available" is contradicting, which one is right 😅
btw i have tried, I'm using kotlin 2.0.20-Beta1 it is not working.
w
You are using
is
instead of
as
🙂
s
But according to this slide it must work
w
Ah I see, this is a different scenario. Here
animal
is declared as
val
, not
var
. (You can't see it on the image, but otherwise it's not possible).
👍 1
s
Oh, you're correct, global var prevents any smart casting
w
(also, I think you mean to use
animal.purr()
instead of
animalIsCat.purr()
🙂
1
s
sorry my bad, here is updated one.
also with val is still not working
w
Hmm interesting, I think think it should actually work... Perhaps they limited the DFA for globals, but I doubt that...
It should work for sure if you inline the
animalIsCat
variable
For me it works on 2.0.0 https://pl.kotl.in/2K04leoPR
s
IDEA does not enable K2 Mode by default. It should probably compile successfully.
j
Check this example in Kotlin: 2.0 https://pl.kotl.in/r2qEGaxmU