Is there a better way to do function calls similar...
# getting-started
m
Is there a better way to do function calls similar to this?
Copy code
someFunction(  // takes varargs
    *arrayOf(if (condition) "someValue" else null).filterNotNull().toTypedArray(),
)
w
Maybe with
listOfNotNull
?
e
Copy code
someFunction(
    *if (condition) arrayOf("someValue") else emptyArray()
)
k
I like ephemient's solution but some people may find the following simpler-looking and more readable:
Copy code
if (condition)
    someFunction("someValue")
else
    someFunction()
2
a
Or extract it out:
Copy code
fun anotherFunction(condition: Boolean) = 
  if (condition)
    arrayOf("someValue")
  else
    emptyArray()
At least then the “condition” is now testable alone, as it would have been buried in the call of another function and therefore obfuscated.
Copy code
someFunction(*anotherFunction(condition))