Spread Operator
In this page:
fun functionName(vararg items: Type) { }
functionName(a, b, c)
functionName(*array) // spread an array
functionName(*list.toTypedArray())
Declaring a vararg Function
A parameter marked vararg accepts any number of individual arguments of that type, which are made available inside the function as an array.
Example: Declaring a vararg Function
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
// Return `numbers.sum()` from this function
return numbers.sum()
}
// Entry point: execution of the program starts here
fun main() {
// Print "Sum: ${sumAll(1, 2, 3, 4)}" to the console, with a trailing newline
println("Sum: ${sumAll(1, 2, 3, 4)}")
}
Login to try C/C++/Java/PHP code in the editor
Spreading an Array into vararg
When you already have an array and want to pass its contents as individual arguments to a vararg parameter, prefix it with * -- this is the spread operator.
Example: Spreading an Array into vararg
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
// Return `numbers.sum()` from this function
return numbers.sum()
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `values`, set to `intArrayOf(10, 20, 30)`
val values = intArrayOf(10, 20, 30)
// Print "Sum: ${sumAll(*values)}" to the console, with a trailing newline
println("Sum: ${sumAll(*values)}")
}
Login to try C/C++/Java/PHP code in the editor
Spreading a List
Since the spread operator works on arrays, a List must first be converted with .toTypedArray() before it can be spread into a vararg parameter.
Example: Spreading a List
// Define the function `joinAll` taking `words` and returning a String
fun joinAll(vararg words: String): String {
// Return `words.joinToString(" ")` from this function
return words.joinToString(" ")
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `wordList`, set to `listOf("Hello", "from", "Kotlin")`
val wordList = listOf("Hello", "from", "Kotlin")
// Print `joinAll(*wordList.toTypedArray())` to the console, with a trailing newline
println(joinAll(*wordList.toTypedArray()))
}
Login to try C/C++/Java/PHP code in the editor
Combining Spread with Other Arguments
A spread array can be mixed with additional individual arguments in the same call, as long as the overall argument order still matches how the function expects them.
Example: Combining Spread with Other Arguments
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
// Return `numbers.sum()` from this function
return numbers.sum()
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `values`, set to `intArrayOf(1, 2, 3)`
val values = intArrayOf(1, 2, 3)
// Print "Sum: ${sumAll(0, *values, 100)}" to the console, with a trailing newline
println("Sum: ${sumAll(0, *values, 100)}")
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
*prefix when passing an array to avarargparameter, causing a type mismatch since a single array is not the same as several individual arguments. - Trying to spread a
Listdirectly; the spread operator works on arrays, so aListmust be converted with.toTypedArray()first. - Assuming spread can mix with other individual arguments incorrectly ordered; extra positional arguments before/after a spread must still respect parameter order rules.
- The
*(spread) operator unpacks an array into individual arguments for avarargparameter. - Without
*, passing an array directly to avarargparameter is a type mismatch. - A
Listmust first be converted to an array with.toTypedArray()before it can be spread. - Spread arguments can be combined with additional individual arguments in the same call.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: