In Java, it is possible to do something like this: void function(Url... urls)
. It makes possible to use 1..n urls. The question is if it is possible to do the same thing with Kotlin.
:: converts a Kotlin function into a lambda. this translates to MyClass(x, y) in Kotlin.
In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.
It's Kotlin's spread operator — the operator that unpacks an array into the list of values from the array. It is needed when you want to pass an array as the vararg parameter. it will fail with Type mismatch: inferred type is Array<String> but String was expected compilation error.
The * operator is known as the Spread Operator in Kotlin. When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):
The solution is with vararg
and the it is possible to iterate over the parameter.
private fun areValidFields(vararg fields: String) : Boolean{
return fields.none { it.isNullOrEmpty() || it.isBlank() }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With