Spreading - Manhunter07/MFL GitHub Wiki
When calling a function or when declaring a type, MFL supports the spreading of one value uppon different parameters. In the context of programming, this is commonly called argument spreading. It allows a function or a type constructor to be called with an unknown amount of arguments during compilation time. Because MFL checks argument counts for calls only when the function is actually called or when a type is constructed, this becomes a possible solution when you need to pass multiple array elemets, string characters or record fields to a function.
When calling a function or when declaring a type that supports spreading, to spread a value instead of passing it directly, use the spread
keyword. It is then followed by an evaluated expression returning the value for spreading.
Supported types
Spreadable values are those that support spreading. This usually includes all structured types like arrays and records. Strings are also supported, because they can be evaluated and implicitely converted into arrays. When spreading a string, it is therefore passed as an array of its characters. Numbers, References and other non-structured types are never supported for spreading and cause an exception when passed.
When an array is spreaded in a function call, its elements define each of the arguments passed to the parameters ordered by their index. The array length must be at least the amount of required and at most the amount of supported arguments. Just like in a regular function call, too many or too few arguments passed raise an exception.
When a record is used for spreading, its fields are used to construct named arguments for the function call. This means that its fields names must be identical to the parameter names of the function, otherwise an exception is raised. As records are unordered and their field names case-insenstivie, the declaration order if their fields and the keys' character-casing are ignored.
Example
function Add(A, B) = A + B
Add(spread [3, 9]) \returns 12\
Add(spread {A = "Hello", B = "World"}) \returns "Hello World"\
Add(spread "Hi") \returns "Hi"\