JetBrains/kotlin — pull request #100 by kouphax (2012-07-16 20:34:01+00:00)
Title: getOrElse Nullable Extensions

Inspired by Scalas `Option` getOrElse this method will return the non-null version of the nullable type unless it is null and thus returns the user defined result passed in.

These are equivalent to the ?: operator except this provides a better syntax when chaining methods. The lambda parameter version allows for lazy evaluation of the result e.g.

// authenticate returns a user if it is valid otherwise returns null
// therefore User? type. We want to return a certain view depending
// on the result
authenticate(username, password).map {
views.html.index(it)
}.getOrElse {
views.html.login("Invalid username or password")
}

This is a somewhat contrived example from the Play! 2 scala framework (written as a kotlin statement)