12.2.2 Return and reception of multiple values

A Dylan function call — and, in general, a Dylan expression — can return any number of values, including none. The values function is the means of returning multiple values. This function takes zero or more arguments, and returns them as separate values.

Multiple values can be received as the initial values of local variables in a let declaration. If a let declaration contains multiple variables, they are matched with the values returned by the initialization expression, and each variable is bound to the corresponding value. The following example initializes a to 1 and b to 2:

let (a, b) = values(1, 2);

The following example initializes ans to 2 and rem to 1 — the two values returned by this call to truncate/:

let (ans, rem) = truncate/(5, 2);

The variable list can also end with #rest followed by the name of a variable. In this case, the variable is initialized to a sequence. This sequence contains all the remaining values returned by the initialization expression. If there is no #rest, any excess values are discarded. If the number of variables in the let declaration is greater than the number of values returned, the remaining variables are initialized to #f. (But if the let declaration specifies a type for any of these variables, and if #f is not an instance of that type, then Dylan signals an error.)

Module variables and constants can also be initialized to multiple values. The variable list of a define variable or define constant definition can contain multiple variables, and can receive multiple values from its initialization expression in the same way as a let declaration.