In Kotlin, I can run code if an object is not null like this:
data?.let {
... // execute this block if not null
}
but how can I execute a block of code if the object is null?
You can use the elvis operator and evaluate another block of code with run { ... }
:
data?.let {
// execute this block if not null
} ?: run {
// execute this block if null
}
But this seems not to be quite as readable as a simple if
-else
statement.
Also, you might find this Q&A useful: