I have a set of some parameters of different types. And I want to do some operation with them. But only if at least of one them is not null.
My code is something like this:
<code>fun <A, B, C, R> atLeastOneNotNull(a: A?, b: B?, c: C?, block: (A?, B?, C?) -> R): R? {
return if (atLeastOneNotNull(a, b) { _, _ -> } != null || c != null) {
block(a, b, c)
} else {
null
}
}
</code>
<code>fun <A, B, C, R> atLeastOneNotNull(a: A?, b: B?, c: C?, block: (A?, B?, C?) -> R): R? {
return if (atLeastOneNotNull(a, b) { _, _ -> } != null || c != null) {
block(a, b, c)
} else {
null
}
}
</code>
fun <A, B, C, R> atLeastOneNotNull(a: A?, b: B?, c: C?, block: (A?, B?, C?) -> R): R? {
return if (atLeastOneNotNull(a, b) { _, _ -> } != null || c != null) {
block(a, b, c)
} else {
null
}
}
But the number of parameters varies in different use cases. In this example we have 3, but it could be 2 or 5.
Is it possible to create a function that would do the same but accept variable amount of parameters?
I have multiple functions for different number of paramaters, but i would like to have just one generic function.