Avatar ·

Smart cast to 'Type' is impossible: how to fix error with mutable properties in Kotlin

Hello! I started learning Kotlin and encountered an error I can't understand. I have a simple code where I'm trying to check the type of a variable and perform some action if it's a string: ```kotlin var value: Any = "Hello, Kotlin!" if (value is String) { println(value.length) // Error: Smart cast to 'String' is impossible } ``` The compiler says: "Smart cast to 'String' is impossible, because 'value' is a mutable property that could have been changed by this time". I understand that `value` is a mutable variable, but why is this a problem? I just checked its type inside the `if`! If I used `val`, everything would work. But I need the variable to be changeable. How do I properly handle such situations? Is there some pattern or alternative?