Tuples are used for creating aggregates of values. They provide a convenient way of lumping together a set of values without having to type in a new class definition first.
Type Declaration
<type1, type2, .., typen>
Example
<int, str>
<str, double, int>
Literal Syntax
<expr1, expr2, .., exprn>
where each exprn is an expression that is a subtype of typen.
Example
<1, "one">
<"configura", 3.14, factorial(3)>
Tuple Class
Tuple declarations create new classes on demand. These classes always belong to the cm.lang package and inherit from Tuple.
Fields
The fields of a tuple are named v1, v2 .. vn, where n is the position of the field in the declaration, counting from left with 0 as the first position. The leftmost field is thus called v0, the second v1, and so on.
Methods
Tuples have one constructor and a method called toS() that returns a human-readable description of the tuple object.
Example
The following tuple type:
<str, int>
is equivalent to this class definition, but unnamed:
use cm.lang; public class ExampleTuple extends Tuple { public str v0; public int v1; public constructor(str v0, int v1) { this.v0 = v0; this.v1 = v1; } public str toS() { return '<' # v0 # ", " # v1 # '>'; } }
Parallel Assignment
The fields of a tuple object can be assigned in parallel using the normal assignment operator.
Example
<str, int> pair = <"one", 1>;
Comments
0 comments
Please sign in to leave a comment.