Created
January 12, 2022 06:56
-
-
Save meowpunch/1c0617137fc9bc3791ccff432e933ed7 to your computer and use it in GitHub Desktop.
Smart casts in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// x is automatically cast to String | |
if (obj is String) { return obj.length } | |
x is String && x.length == 0 | |
// compile error | |
x is String || x.length == 0 | |
// with `when` expression | |
when (x) { | |
is String -> x.length | |
else -> -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment