Recently I read about the smart cast performed by the is operator and also about the as or the better as?operators in that is used for explicit casting.
The kotlin docs species their difference of usage as follows:-
Note that smart casts do not work when the compiler cannot guarantee that the variable cannot change between the check and the usage. More specifically, smart casts are applicable according to the following rules:
val local variables - always except for local delegated properties;
val properties - if the property is private or internal or the check is performed in the same module where the property is declared. Smart casts aren't applicable to open properties or properties that have custom getters;
var local variables - if the variable is not modified between the check and the usage, is not captured in a lambda that modifies it, and is not a local delegated property;
var properties - never (because the variable can be modified at any time by other code).
Note that smart casts do not work when the compiler cannot guarantee that the variable cannot change between the check and the usage.
The above written is bit confusing, since the var variables can be changed after initialization and I could not find a example that can make it clear the actual insight of the statement.
Can anyone make it easier to understand this insight better, in anyway?
And does is operator provide some optimization benefit over the as operator if, any?