Skip to content

Instantly share code, notes, and snippets.

@navczydev
Created December 8, 2022 00:49
Show Gist options
  • Save navczydev/e8ba8324690b36b775ad78de305df8ec to your computer and use it in GitHub Desktop.
Save navczydev/e8ba8324690b36b775ad78de305df8ec to your computer and use it in GitHub Desktop.
fun processNullableName(name: String?) {
// Force unwrap !!
println("Name is: ${name!!}")
// Elvis operator ?:
println("Name is: ${name ?: "Kotlin"}")
// If statement
if (name != null) {
// Kotlin is smart enough to infer the type of name here as String 😎
print("Name is: $name")
}
// Scope functions(let, also, etc.)
name?.let { nonNullableName ->
// Kotlin is smart enough to infer the type of name here as String 😎
print("Name is: $nonNullableName")
}
// Chaining
val kotlin: Language? = Language("Kotlin")
print("Name is: ${kotlin?.name}")
}
data class Language(val name: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment