nullfold
Syntax-sugar for a lambda folds a nullable receiver type into type Out.
Looks dumb, but weirdly useful in the right context.
val result: String? = null
// Before:
result?.length ?: 0
// Before:
when (result) {
null -> 0
else -> result.length
}
// After:
result.nullfold(none = { 0 }, some = { it.length })
Content copied to clipboard
Return
Parameters
In
The type of the receiver.
Out
The type of the return value.
none
The lambda to call if the receiver is null
.
some
The lambda to call if the receiver is not null
.