skip to content

Can you mix a spread array with individual literal arguments in a single call to a vararg function? Show what the callee receives and in what order.

level: middleimportance: should knowfreq 45%

answer

  1. Mix literals and *spreads freely in one call
  2. Combined in exact source (left-to-right) order
  3. Multiple spreads per call allowed
  4. f(1, *arr, 4) -> [1, ...arr, 4]
  5. Callee gets one array; origin is invisible

basics

~10 s

Yes. In one call you can write individual values and spread arrays together, like f(0, *arr, 9). Kotlin combines them in the exact left-to-right order you wrote them into one array for the function.

solid answer

~30 s

Kotlin lets you freely interleave individual arguments and spread arrays in the same vararg call: `asList(0, *middle, 9, *tail)`. The compiler concatenates everything in **source order** into the single varargs array the callee receives, so `f(1, *arrayOf(2,3), 4)` yields `[1, 2, 3, 4]`. You can use multiple spreads and place literals before, between, or after them. Each spread must match the vararg's array type. This is the key advantage of Kotlin's spread over languages where the spread must be the last/only argument. Under the hood the compiler may build the combined array via a spread builder, copying elements from each source.

code

kotlin · 4 lines
kotlin
fun all(vararg xs: Int): List<Int> = xs.toList()
val mid = intArrayOf(2, 3)
println(all(1, *mid, 4))         // [1, 2, 3, 4]
println(all(*mid, 1, *mid))      // [2, 3, 1, 2, 3]

go deeper

for a junior

Knows you can write f(1, *arr, 2) and it works.

for a middle

Explains exact source-order concatenation and multiple-spread support.

for a senior

Notes each spread is copied into one combined array and type-matching per spread.

for a principal

Considers allocation cost of building combined arrays in hot code and API shape choices to avoid it.

## Mixing is allowed and order-preserving Unlike some languages where a spread must be the sole or trailing argument, Kotlin lets you **interleave** plain arguments and any number of spreads in one vararg call. The compiler assembles them into a single array in **exact left-to-right source order**. ```kotlin fun listAll(vararg xs: Int): List<Int> = xs.toList() val mid = intArrayOf(2, 3) val tail = intArrayOf(6, 7) listAll(1, *mid, 4, 5, *tail) // [1, 2, 3, 4, 5, 6, 7] ``` ## Rules and details - **Any mix**: literals before, between, and after spreads; multiple spreads in one call are fine. - **Order is preserved** exactly as written — there is no reordering. - Each **spread argument's type** must match the vararg array type (`IntArray` here; `Array<out T>` for reference varargs). - The result is **one** varargs array; the callee cannot tell which elements came from a spread vs a literal. ## Reference-type example ```kotlin fun cmd(vararg parts: String): String = parts.joinToString(" ") val flags = arrayOf("-v", "-x") cmd("git", *flags, "status") // "git -v -x status" ``` ## How it compiles When a call contains spreads, the compiler emits code that builds the combined array (conceptually a spread/array builder that appends each literal and copies each spread's contents) before invoking the function. Each element is copied once into the final array. ## Practical use This is ideal for **building command lines, query argument lists, or message parts** where you have a fixed prefix/suffix and a dynamic middle array.

  • Is the order guaranteed?
    Yes — elements appear in the exact left-to-right order written, with each spread expanded in place.
  • Can you use two different arrays' spreads in one call?
    Yes, `f(*a, *b)` is valid as long as both match the vararg's array type.

saying these in an interview costs you the question

  • Claiming the spread must be the only/last argument
  • Thinking literals always come before spreads regardless of position
  • Believing only one spread is allowed per call
  • Assuming the callee can distinguish spread vs literal elements

context