Last active
July 1, 2019 08:52
-
-
Save Temidtech/c983b7f6fd5df9a0083f25ffd0864962 to your computer and use it in GitHub Desktop.
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
// 1. Smart cast example | |
fun getUserName(): String? = ... | |
val username = getUserName() | |
if (username != null) { | |
println(username.length) | |
} | |
// 2. Another Example | |
fun getName(obj: Any?){ | |
if (obj == null || obj !is String) | |
{ return } | |
val string = obj println("Length of the string is ${string.length}") | |
} | |
getName(null) | |
getName("Black Panther") | |
//Compiler output | |
Length of the string is 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment