Goose  Tuples

A tuple is a list of values of different types, built using the comma operator. Every list of values separated by commas in goose, including function parameter declarations and function call arguments are tuples.

Open and closed tuples

A tuple enclosed by parentheses is a closed tuple. A tuple without parentheses is an open tuple.

The comma operator, when used with two values that aren't open tuples, creates a new open tuple of those two values. If used with two open tuples, it concatenates them, and if used with one open tuple and something else, it will either prepend or append that something else to the tuple.

Tuple based language features

The simplicity of creating tuples enables a few useful features. Those are inspired from lua.

Multiple return values

The return type of a function can be a tuple of types, and the return expression a tuple of values:

bool, int, string lomarf()
    return true, 123, "blah"

Since the comma operator concatenates open tuples, the values returned by a function can be spliced directly into the results of another one:

int, bool, int, string, bool laffo()
    return 456, lomarf(), false
Destructuring tuples with variable declarations

A variable declaration (a type or template expression followed by a name), like most goose grammatical constructs, is itself a value, so it is possible to put several of them in a tuple and initalize them all at once with a tuple of value:

bool a, var b, string c = lomarf()

Gets the three values returned by lomarf() in the newly declared variables a, b and c.

Tuple assignment

Since local variables are themselves compilation time values, it is possible to put them in a tuple and assign them all at once with a tuple of values. The tuple's implementaiton of the = operator first evaluates all the values on the right hand side, load them into temporary variables, and then assign them to the variables on the left hand side.

This allows tuple assignments to be used to exchange or permute variables easily:

a,b = b,a // swaps a and b

It can also be useful to perform computations where several variables must both be updated according to the previous value of those same variables:

int a = 5
int b = 10

a,b = a+b, a+b  // Will set both a and b to 15

Without tuple assignment, a temporary variable would need to have been declared and used explicitely (note: even with the tuple assignment, this is what happens internally)