Created
November 17, 2019 22:18
-
-
Save magdamiu/83f12f3f3b8ec23db3a954c5174f8b8e to your computer and use it in GitHub Desktop.
null safety with conditional if
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
// check for null in conditions | |
val trainingName: String? = "Learn Kotlin in 45 minutes" | |
if (trainingName != null && trainingName.isNotEmpty()) { | |
print("String of length ${trainingName.length}") | |
} else { | |
print("Empty string") | |
} | |
val platform: String? = null | |
val language = "Kotlin" | |
println(platform?.length) // safe call | |
println(language.length) // unnecessary safe call | |
val lengthOfWord = platform!!.length // !! operator | |
val numberOfLetters: Int? = lengthOfWord as? Int // safe cast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment