Spread Operator

The spread operator ... expands a collection’s contents into a new context. It works with lists, maps, and structs.

List Spread

Use ...list inside a list literal to expand its elements into the new list.

Combining lists

list spread
var a = [1, 2]
var b = [3, 4]
var combined = [...a, ...b, 5]
// [1, 2, 3, 4, 5]

Spreading empty lists

Spreading an empty list adds nothing.

var empty = []
var result = [0, ...empty, 1]
// [0, 1] — length is 2

Nested spread

Spread can be nested inside list literals.

var nested = [0, ...[1, ...[2, 3]], 4]
// [0, 1, 2, 3, 4]

Function arguments

Spread a list into individual function arguments.

var args = [1, 2, 3]
func sum(a, b, c) { return a + b + c }
sum(...args)    // 6

Map Spread

Use ...map inside a map literal to copy all key-value pairs into the new map.

Combining maps

map spread
var a = { a: 1, b: 2 }
var b = { b: 3, c: 4 }
var combined = { ...a, ...b, d: 5 }
// { a: 1, b: 3, c: 4, d: 5 }

Override order

When keys conflict, later values win. This applies to both spread-vs-spread and spread-vs-literal.

override order
var base = { a: 1, b: 2 }

// Literal before spread — spread wins
var m1 = { a: 100, ...base }
m1.a    // 1 — base.a overrode the literal

// Spread before literal — literal wins
var m2 = { ...base, a: 999 }
m2.a    // 999 — literal overrode base.a

Struct Spread

Use ...struct inside a named struct initializer to copy fields from an existing instance, then selectively override specific fields.

Copying with overrides

struct spread
struct Point { x; y; z }

var p1 = Point{.x = 1, .y = 2, .z = 3}
var p2 = Point{...p1, .z = 10}
p2.x    // 1  — copied from p1
p2.y    // 2  — copied from p1
p2.z    // 10 — overridden

Override order (structs)

Like maps, later values win when spread and literal fields conflict.

var p1 = Point{.x = 1, .y = 2, .z = 3}

// Literal before spread — spread wins
var p3 = Point{.x = 0, ...p1}
p3.x    // 1 — spread overrode the literal

// Spread before literal — literal wins
var p4 = Point{...p1, .x = 0}
p4.x    // 0 — literal overrode the spread

Partial updates

Spread is ideal for creating modified copies of structs without repeating all fields.

partial update
struct Config { host; port; debug; timeout }

var defaults = Config{
    .host = "localhost",
    .port = 8080,
    .debug = false,
    .timeout = 30
}

// Only change what you need
var dev = Config{...defaults, .debug = true}
var prod = Config{...defaults, .host = "prod.example.com", .port = 443}

Summary

ContextSyntaxEffect
List literal[...list, elem]Expands list elements into the new list
Function callfn(...args)Expands list into individual arguments
Map literal{...map, key: val}Copies all key-value pairs; later wins on conflict
Struct initializerType{...inst, .field = val}Copies all fields; later wins on conflict