3.3. Variable-arity functions

Functions may take a varying number of parameters. To define one, just add ... to the last parameter name:

function foo = |a, b, c...| {
  # ...
}

Here, c catches the variable arguments in an array, just like it would be the case with Java. You can thus treat c as being a Java object of type Object[].

Calling variable-arity functions does not requiring wrapping the last arguments in an array. While invoking the foo function above, the following examples are legit:

# a=1, b=2, c=[]
foo(1, 2)

# a=1, b=2, c=[3]
foo(1, 2, 3)

# a=1, b=2, c=[3,4]
foo(1, 2, 3, 4)

Because the parameter that catches the last arguments is an array, you may call array methods. Given:

function elementAt = |index, args...| {
  return args: get(index)
}

then:

# prints "2"
println(elementAt(1, 1, 2, 3))