Skip to content

Instantly share code, notes, and snippets.

@meowpunch
Created January 12, 2022 06:56
Show Gist options
  • Save meowpunch/1c0617137fc9bc3791ccff432e933ed7 to your computer and use it in GitHub Desktop.
Save meowpunch/1c0617137fc9bc3791ccff432e933ed7 to your computer and use it in GitHub Desktop.
Smart casts in Kotlin
// 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