|
1 | 1 | # Smart Casts |
2 | 2 |
|
3 | 3 |
|
4 | | -The Kotlin compiler is smart enough to perform type casts automatically in most cases, including |
5 | | -1. Casts from nullable types to their non-nullable counterparts |
6 | | -2. Casts from a supertype to a subtype |
| 4 | +The Kotlin compiler is smart enough to perform type casts automatically in most cases, including: |
| 5 | +1. Casts from nullable types to their non-nullable counterparts. |
| 6 | +2. Casts from a supertype to a subtype. |
7 | 7 |
|
8 | 8 | <div class="language-kotlin" theme="idea" data-min-compiler-version="1.3"> |
9 | 9 |
|
@@ -38,11 +38,10 @@ fun main() { |
38 | 38 |
|
39 | 39 | </div> |
40 | 40 |
|
| 41 | +1. Declaring a nullable variable. |
| 42 | +2. Smart-cast to non-nullable (thus allowing direct access to `isLeapYear`). |
| 43 | +3. Smart-cast inside a condition (this is possible because, like Java, Kotlin uses [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation)). |
| 44 | +4. Smart-cast inside acondition (also enabled by short-circuiting). |
| 45 | +5. Smart-cast to the subtype `LocalDate`. |
41 | 46 |
|
42 | | -1. Declares a nullable variable |
43 | | -2. Smart-cast to non-nullable (thus allowing direct access to `isLeapYear`) |
44 | | -3. Smart-cast inside condition (this is possible because, like Java, Kotlin uses [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation)) |
45 | | -4. Smart-cast inside condition (also enabled by short-circuiting) |
46 | | -5. Smart-cast to subtype LocalDate |
47 | | - |
48 | | -This way, you can automatically use variables as desired most of the time without doing obvious casts manually. |
| 47 | +This way, you can automatically use variables as desired in most cases without doing obvious casts manually. |
0 commit comments