The * operator is known as the Spread Operator in Kotlin.
From the Kotlin Reference:
When you call a
vararg-function, you can pass arguments individually, for exampleasList(1, 2, 3). if you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with*):
It can be applied to an Array before passing it into a function that accepts vararg.
For Example...
If you have a function that accepts a varied number of arguments...
fun sumOfNumbers(vararg numbers: Int): Int {
return numbers.sum()
}
Use the spread operator to pass an array's elements as the arguments:
val numbers = intArrayOf(2, 3, 4)
val sum = sumOfNumbers(*numbers)
println(sum) // Prints '9'
Notes:
- The
*operator is also the multiplication operator (of course). - The operator can only be used when passing arguments to a function. The result of the operation cannot be stored since it yields no value (it is purely syntactic sugar).
- The operator may confuse some C/C++ programmers at first because it looks like a pointer is being de-referenced. It isn't; Kotlin has no notion of pointers.
- The operator can be used in-between other arguments when calling a
vararg-function. This is demonstrated in the example here. - The operator is similar to the
applyfunction in various functional programming languages.
The * operator is known as the Spread Operator in Kotlin.
From the Kotlin Reference:
When you call a
vararg-function, you can pass arguments individually, for exampleasList(1, 2, 3). if you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with*):
It can be applied to an Array before passing it into a function that accepts vararg.
For Example...
If you have a function that accepts a varied number of arguments...
fun sumOfNumbers(vararg numbers: Int): Int {
return numbers.sum()
}
Use the spread operator to pass an array's elements as the arguments:
val numbers = intArrayOf(2, 3, 4)
val sum = sumOfNumbers(*numbers)
println(sum) // Prints '9'
Notes:
- The
*operator is also the multiplication operator (of course). - The operator can only be used when passing arguments to a function. The result of the operation cannot be stored since it yields no value (it is purely syntactic sugar).
- The operator may confuse some C/C++ programmers at first because it looks like a pointer is being de-referenced. It isn't; Kotlin has no notion of pointers.
- The operator can be used in-between other arguments when calling a
vararg-function. This is demonstrated in the example here. - The operator is similar to the
applyfunction in various functional programming languages.
In addition to the answers that were directly towards "what is this thing!?!", you often have the case where you have a List and want to pass it to a function that is expecting a vararg. For this, the conversion is:
someFunc(x, y, *myList.toTypedArray())
Assuming that last parameter of someFunc is vararg of the same type as the elements in the list.
Help me understand the spread operator
Spread operator in Python and Kotlin are really much different things. Python lets you pass any positional arguments using an array. Kotlin lets you only pass vararg arguments.
And the reason why only arrays are supported is because vararg is internally represented as just array. So * in Kotlin does not really unpack anything. Actually, it is the opposite. If you declare a function with vararg then passed arguments are packed into an array. By using * you can pass an array directly.
Spread-object operator missing in kotlin? - Language Design - Kotlin Discussions
Allocation of vararge with spread operator - Language Design - Kotlin Discussions
Spread operator not working on vararg indexed access operator - Language Design - Kotlin Discussions
What does 'vararg' do in Kotlin?
How to pass array to varargs in Kotlin?
How many varargs can we have in a function in Kotlin?
Hey all,
Been learning Kotlin (my background is Python)!
I was pretty happy to see that Kotlin has a very similar operation to unpack a collection of items (*).
That being said, I couldn't really find the exact reason why the spread operator only supports Arrays and not other types of collections like Lists? From my understanding, the main difference between List and and arrays is that Arrays are mutable whereas Lists are not by default (you'd have to call a mutableListOf), and arrays are fixed in size.
Where does this limitation come from?
Spread operator in Python and Kotlin are really much different things. Python lets you pass any positional arguments using an array. Kotlin lets you only pass vararg arguments.
And the reason why only arrays are supported is because vararg is internally represented as just array. So * in Kotlin does not really unpack anything. Actually, it is the opposite. If you declare a function with vararg then passed arguments are packed into an array. By using * you can pass an array directly.
To add to the previous replies, this is (*) helpful when using java SDK's which has functions accepting vararg and you already have a kotlin array you want to pass in.