Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A vararg (variable number of arguments) parameter allows a function to accept zero or more arguments of a specified type. Declared using the vararg modifier, it instructs the Kotlin compiler to pack the comma-separated arguments provided at the call site into an array accessible within the function body.

Syntax and Type Mapping

When you declare a vararg parameter of type T, the compiler translates it into an Array<out T> within the function scope.
fun processStrings(vararg elements: String) {
    // 'elements' is treated as Array<out String>
    println(elements.size)
}

processStrings("Alpha", "Beta", "Gamma")
processStrings() // Valid: results in an empty array
To prevent the performance overhead of boxing and unboxing, Kotlin optimizes vararg parameters for primitive types. A vararg of a primitive type compiles directly to its corresponding specialized primitive array (e.g., IntArray, DoubleArray, CharArray).
fun processIntegers(vararg numbers: Int) {
    // 'numbers' is treated as IntArray, not Array<Int>
}

The Spread Operator (*)

If you already have an array and want to pass its contents to a function expecting a vararg parameter, you must unpack the array using the spread operator (*). Passing the array directly will result in a type mismatch, as the function expects individual elements, not the array reference itself.
val stringArray = arrayOf("Delta", "Echo")

// Unpacking the array elements into the vararg parameter
processStrings(*stringArray) 

// The spread operator can be combined with discrete arguments
processStrings("Alpha", *stringArray, "Foxtrot")
Note: The spread operator only works with arrays. To pass a List or other Collection, it must first be converted using .toTypedArray().
val stringList = listOf("Golf", "Hotel")
processStrings(*stringList.toTypedArray())

Parameter Positioning and Resolution Rules

Kotlin enforces specific rules regarding the placement of a vararg parameter within a function signature:
  1. Single Vararg Limit: A function can declare a maximum of one vararg parameter.
  2. Trailing Parameters: While it is conventional to place the vararg parameter at the end of the parameter list, it is not strictly required. However, if parameters follow a vararg, they must be passed using named arguments at the call site to resolve ambiguity.
fun configure(vararg flags: String, isEnabled: Boolean) {
    // Implementation
}

// 'isEnabled' must be explicitly named
configure("DEBUG", "VERBOSE", isEnabled = true)
If a function signature includes a functional parameter (lambda) following a vararg, trailing lambda syntax can be used without explicitly naming the parameter.
fun execute(vararg commands: String, action: () -> Unit) {
    // Implementation
}

execute("START", "RUN") {
    println("Executing action")
}
Master Kotlin with Deep Grasping Methodology!Learn More