Spread syntax injects tuples and records into function calls.
Discussion
Functions have a static arity, that is, the number of parameters is known at compile-time. That number may be a range, if the function has optional parameters, but it’s still a static range. When a function is called, rather than providing arguments one by one, we can spread a tuple or record into the arguments list. Since the arity of the function is static, we can’t spread arrays or sets into its arguments. We also can’t spread mappings into function arguments.
We will use the following example to demonstrate:
func joinStrings(a: str, b: str, c: str, d: str, e: str, f: str): str
=> '''{{ a }}{{ b }}{{ c }}{{ d }}{{ e }}{{ f }}''';
Tuple Spread
Tuples may be spread into function call positional arguments with the # symbol. Spreading a tuple into a function call is equivalent to providing each positional argument individually.
let cde: [str, str, str] = ['c', 'd', 'e'];
let result: str = joinStrings.('a', 'b', #cde, 'f')~;
assert result == joinStrings.('a', 'b', cde.0, cde.1, cde.2 , 'f')~;
Record Spread
Records may be spread into function call named arguments with the ## symbol. Spreading a record into a function call is equivalent to providing each named argument individually. The evaluation order of named arguments in the spread record is that record’s insertion order.
Notice that spreading a record into named function arguments is similar to destructuring (#48), except that we don’t have to list out all the properties:
Since tuples spread into positional arguments and records spread into named arguments, there are restrictions of the order these syntaxes can appear in a function call: All positional arguments and tuple spreads must come before all named arguments and record spreads. If any named argument or record spread appears before any positional argument or tuple spread in a function call, then it’s a syntax error.
my_function.(a= 42, 420)~; % ParseError: positional argument cannot appear after named argument
my_function.(a= 42, #[420])~; % ParseError: tuple spread cannot appear after named argument
my_function.(##[a= 42], 420)~; % ParseError: positional argument cannot appear after record spread
my_function.(##[a= 42], #[420])~; % ParseError: tuple spread cannot appear after record spread
However, spread tuples and positional arguments may appear intermixed, as may spread records and named arguments.
Spread syntax injects tuples and records into function calls.
Discussion
Functions have a static arity, that is, the number of parameters is known at compile-time. That number may be a range, if the function has optional parameters, but it’s still a static range. When a function is called, rather than providing arguments one by one, we can spread a tuple or record into the arguments list. Since the arity of the function is static, we can’t spread arrays or sets into its arguments. We also can’t spread mappings into function arguments.
We will use the following example to demonstrate:
Tuple Spread
Tuples may be spread into function call positional arguments with the
#
symbol. Spreading a tuple into a function call is equivalent to providing each positional argument individually.Record Spread
Records may be spread into function call named arguments with the
##
symbol. Spreading a record into a function call is equivalent to providing each named argument individually. The evaluation order of named arguments in the spread record is that record’s insertion order.Notice that spreading a record into named function arguments is similar to destructuring (#48), except that we don’t have to list out all the properties:
Limitations
Since tuples spread into positional arguments and records spread into named arguments, there are restrictions of the order these syntaxes can appear in a function call: All positional arguments and tuple spreads must come before all named arguments and record spreads. If any named argument or record spread appears before any positional argument or tuple spread in a function call, then it’s a syntax error.
However, spread tuples and positional arguments may appear intermixed, as may spread records and named arguments.
Specification
Syntax
Semantics
Decorate