The elvis operator (?:) allows you to assign a "default" value if the variable to the left of the elvis operator is null
The notation someVariable?.let{ } allows you to take an action (which is defined within "let") only if someVariable is non-null
fun main() {
// the elvis operator
val test = null
val test2 = test ?: "it was null"
println(test2)
// taking action only if variable is non-null
// using ?.let
val isNull = null
isNull?.let{
println("Inside let with null - will not be printed")
}
val isNotNull = 1
isNotNull?.let{
println("Inside let with non-null value of '$it' - will be printed")
}
}
Output:
it was null
Inside let with non-null value of '1' - will be printed
The elvis operator (?:) allows you to assign a "default" value if the variable to the left of the elvis operator is null
The notation someVariable?.let{ } allows you to take an action (which is defined within "let") only if someVariable is non-null
Output: