Skip to content

Instantly share code, notes, and snippets.

@Temidtech
Last active July 1, 2019 08:52
Show Gist options
  • Save Temidtech/c983b7f6fd5df9a0083f25ffd0864962 to your computer and use it in GitHub Desktop.
Save Temidtech/c983b7f6fd5df9a0083f25ffd0864962 to your computer and use it in GitHub Desktop.
// 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