Jason deBono
05/07/2021, 3:55 AMlistOfNotNull(
if (somethingNullable == null) {ReturnValue} else null,
...
)
Currently I have an inline function:
inline fun <T, R> T?.whenNull(block: () -> R): R? = if (this == null) block() else null
which I use like this:
listOfNotNull(
somethingNullable.whenNull {ReturnValue},
...
)
Is there a better way?
Thanks in advancenanodeath
05/07/2021, 4:11 AMnanodeath
05/07/2021, 4:12 AMlistOfNotNull(
ReturnValue.takeIf { somethingNullable == null }
)
?Francesc
05/07/2021, 4:22 AMnanodeath
05/07/2021, 4:24 AMsomethingNullable ?: run { add(ReturnValue) }
Jason deBono
05/07/2021, 4:27 AMJason deBono
05/07/2021, 4:46 AMbuildList
in combination with ?:
might do the trick. Thanks