Camshaft54
12/12/2021, 4:54 PM// getVal() returns a nullable value
fun example() {
val myVal = getVal() ?: {
println("value is null")
return
}
}
Joffrey
12/12/2021, 4:56 PM{ ... }
is not considered as a block of code here, but as a lambda. You might want to try run { ... }
insteadCamshaft54
12/12/2021, 4:57 PMYoussef Shoaib [MOD]
12/12/2021, 5:00 PMFrancesc
12/12/2021, 7:22 PMalso
Joffrey
12/12/2021, 11:33 PMalso
here? The idea is to print when the expression is null, not when it's not null. Using getVal()?.also { ... }
would do the opposite. So I guess elvis + run block is the way to go here.