Is there an idiomatic way to write something like ...
# announcements
j
Is there an idiomatic way to write something like this?
Copy code
listOfNotNull(
    if (somethingNullable == null) {ReturnValue} else null,
    ...
)
Currently I have an inline function:
Copy code
inline fun <T, R> T?.whenNull(block: () -> R): R? = if (this == null) block() else null
which I use like this:
Copy code
listOfNotNull(
    somethingNullable.whenNull {ReturnValue},
    ...
)
Is there a better way? Thanks in advance
n
sorry that wasn't equivalent...hm
Copy code
listOfNotNull(
  ReturnValue.takeIf { somethingNullable == null }
)
?
n
that would let you say
somethingNullable ?: run { add(ReturnValue) }
j
In my case the ReturnValue is actually a new data class instance. Using takeIf will cause a lot of unnecessary work
I think
buildList
in combination with
?:
might do the trick. Thanks