A tuple is a hotch-botch of values. For example, (123, 3.1415)
is a tuple, and its type is (int, float)
.
The tuple type is not a first-class type: it is impossible to declare a
variable with a tuple type. A single element tuple type actually is the
type of the element. The default value of a tuple type is a tuple with
as elements the default values of the tuple type's elements. Tuples can
nest: ((1, 2), (3, 4))
is a valid tuple, the type of which is
((int, int), (int, int))
.
The dynamic
type can not be an element of a tuple type. All
other types are allowed.
The primary use of tuple types is in passing values to or from a method.
The following example declares a method divmod
which accepts one
argument being a tuple of two integers and which returns another tuple
of two integers:
<doc> Return (a / b, a % b). </doc> (int, int) divmod (int, int) (a, b);
Another use of tuples is in shorthands such as simultaneous assignments:
int a, b; ...; (a, b) = (b, a)
The evaluation of tuple elements is defined to be from left to right. Thus, the result of the following expression
{ int i = 0; (++i, ++i) }
is defined and equal to (1, 2)
.
Syntax
tuple_type: `(' entity_type_list `)' ; entity_type_list: entity_type [`,' entity_type_list] ;
The type of an element of a tuple can be neither dynamic
nor
void
.
Go to the first, previous, next, last section, table of contents.