Skip to content

Spread operator (...) #103

@tmteam

Description

@tmteam

Spread operator ...

Four usage contexts:

1. Variadic function params (rest)

fun foo(a, b, ...c)          # c: T[] — captures remaining args
fun foo(a, b, ...c:int[])    # with explicit type

...c must be the last parameter. Type is array.

2. Array spread

x = [1,2,3]
y = [4,5,6]
z = [...x, ...y]             # [1,2,3,4,5,6]

Expands array inside array literal. Element types must be compatible.

3. Function call spread

foo(1, 2, ...z)              # z: int[] → expanded into individual arguments

Passes array elements as individual arguments. Binds to variadic parameter.

4. Struct spread

user = { name: 'Masha', age: 14 }
copy = { ...user }           # shallow copy
merged = { ...user, age: 15 } # field override

Copies struct fields. Fields after spread override earlier ones.

Implementation areas

  • Tokenizer — new TokType.Spread (...)
  • Parser — spread in array literal, function call, struct literal, param declarations
  • TIC — variadic params, spread type propagation
  • Runtime — array concat, struct merge, rest arg collection
  • Errors — spread of non-array in array context, spread of non-struct in struct context, rest param must be last

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions