Marc Knaup
02/07/2020, 7:36 AM.ifNull { … }
would be awesome indeed :)
https://kotlinlang.slack.com/archives/C0B8Q383C/p1481236430000013
?:
requires weird brace positioning and makes functional code less readable.
inline fun <T : Any> T?.ifNull(onNull: () -> T): T {
contract {
callsInPlace(onNull, InvocationKind.AT_MOST_ONCE)
}
return if (this !== null) this else onNull()
}
Mike
02/07/2020, 11:44 AMifNull/?:
will return an error
, and that gets passed to the also
?Marc Knaup
02/07/2020, 12:08 PMNothing
because error()
always throws. So it will work just fine here 🙂