Previous section: Checking Types

Dylan reference manual -- Binding Multiple Values

Binding Multiple Values

Evaluating a Dylan expression can yield one value, more than one value, or no values at all. This capability is called multiple values.

Multiple values are supported through two capabilities:

values   #rest the-values   =>  the-values	[Function]
Returns the-values as multiple values.
? values(1, 2, 3);
1	// first value returned
2	// second value returned
3	// third value returned
When variables are initialized to values returned by an init in a statement such as let, define variable, and define constant, the number of variables and the number of values are compared:

* If there are the same number of variables and values, the variables are initialized to the corresponding values.

? begin
    let (foo, bar, baz) = values (1, 2, 3);
    list (foo, bar, baz)
  end
#(1, 2, 3)
? define method opposite-edges (center :: <number>,
                                radius :: <number>);
    let (min, max) = edges (center, radius);
    values (max, min);
  end method;
opposite-edges
? opposite-edges (100, 2);
102
98
* If there are more variables than there are values returned by init , the remaining variables are initialized to #f. (If a specialized variable defaults to #f, and #f is not an instance of that variable's type, an error is signaled.)

* If there are more values returned than there are variables, the excess values are placed in a sequence which is used as the initial value for rest-variable; if there is no rest-variable, these excess values are discarded.

? begin
    let (#rest nums) = edges (100, 2);
    nums;
  end
#(98, 102)
* If there is a rest-variable but there are no excess values, rest-variable is initialized to an empty sequence.

Multiple values can be used to perform parallel binding:
? begin
    let x = 10;
    let y = 20;
    let (x, y) = values (y, x);
    list (x, y);
  end
#(20, 10)

Next section: Assignment