How do I run a block of code if a nullable type 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 ifelse statement.

Also, you might find this Q&A useful:

  • In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.