`.ifNull { … }` would be awesome indeed :) <https:...
# stdlib
m
.ifNull { … }
would be awesome indeed :) https://kotlinlang.slack.com/archives/C0B8Q383C/p1481236430000013
?:
requires weird brace positioning and makes functional code less readable.
Copy code
inline fun <T : Any> T?.ifNull(onNull: () -> T): T {
	contract {
		callsInPlace(onNull, InvocationKind.AT_MOST_ONCE)
	}

	return if (this !== null) this else onNull()
}
5
m
I like the clarity/readability of that. Although I don't think your example will work correctly, will it? The
ifNull/?:
will return an
error
, and that gets passed to the
also
?
m
It returns
Nothing
because
error()
always throws. So it will work just fine here 🙂