
上QQ阅读APP看书,第一时间看更新
Smart casts
Smart casting converts a variable of one type to another type, but as opposed to safe casting, it is done implicitly (we don't need to use the as or as? cast operator). Smart casts work only when the Kotlin compiler is absolutely sure that the variable will not be changed after checking. This makes them perfectly safe for multithreaded applications. Generally, smart casts are available for all immutable references (val) and for local mutable references (var). We have two kinds of smart cast:
- Type smart casts cast an object of one type to an object of another type
- Nullity smart casts cast nullable references to non-nullable