LEVIATHAN vd52e79c4 · d52e79c486

Standard Library

struct Triple<A, B, C>

A group of three values, possibly of different types.

since 0.1.0-alpha.1linuxwindowswasm

Overview

The three-field sibling of Pair, with the same conventions: it is a value type that is copied on assignment, and two triples are equal when all three fields are equal. Create one with Triple::Of(first, second, third). Use it when three values travel together and a purpose-built struct would be more than you need.

Description

Triple<A, B, C> holds three values in the fields first, second and third. Build one with Triple::Of(a, b, c). It follows the same conventions as Pair: it is a struct and therefore a value, copied on assignment, compared field by field with ==, and printed by toString() as (first, second, third).

Use a Triple where three values travel together briefly, such as a coordinate pair and a label, or a key with two related values. It saves writing a one-off struct and avoids nesting a Pair<A, Pair<B, C>>. When the three values carry a lasting meaning, a struct with named fields is clearer.

A point with a label

use io::console;

Triple<int, int, string> point = Triple::Of(3, 4, "home");
console.writeln(point.first + point.second);
console.writeln(point.third);
console.writeln(point.toString());
Triple<int, int, string> moved = point;
moved.first = 10;
console.writeln(point.first);
console.writeln(moved.first);
console.writeln(point == Triple::Of(3, 4, "home"));
console.writeln(point == moved);
7
home
(3, 4, home)
3
10
true
false

Examples

An array of triples

use io::console;

Array<Triple<string, int, bool>> rows = [Triple::Of("ann", 30, true), Triple::Of("bob", 25, false)];
for (Triple<string, int, bool> row in rows) {
    console.writeln("${row.first} ${row.second} ${row.third}");
}
console.writeln(rows[1].toString());
ann 30 true
bob 25 false
(bob, 25, false)

Examples

Typical use

use io::console;

Triple<string, int, float> row = Triple::Of("widget", 4, 2.5);
console.writeln("${row.first} x${row.second} @ ${row.third}");
console.writeln(row.toString());
console.writeln(row == Triple::Of("widget", 4, 2.5));
console.writeln(row == Triple::Of("widget", 5, 2.5));
widget x4 @ 2.5e0
(widget, 4, 2.5e0)
true
false

Constructors

Of

Triple::Of(A a, B b, C c)

Create a triple from its three values.

Written Triple::Of(a, b, c); the type arguments are inferred from the declared type of the variable it is assigned to.

Parameters

a
The value for first.
b
The value for second.
c
The value for third.

Examples

use io::console;

Triple<int, string, float> t = Triple::Of(1, "x", 2.5);
console.writeln(t.first);
console.writeln(t.second);
console.writeln(t.third);
1
x
2.5e0

Fields

first

A first

The first value of the triple.

second

B second

The second value of the triple.

third

C third

The third value of the triple.

Methods

toString

toString() -> string

The triple as text, in the form (first, second, third).

This is what ${} interpolation of a triple produces. The fields are joined with +, so a field that is itself a struct is not expanded.

Returns

The text (first, second, third).

Examples

use io::console;

Triple<int, int, int> t = Triple::Of(1, 2, 3);
console.writeln(t.toString());
console.writeln("t = ${t}");
(1, 2, 3)
t = (1, 2, 3)

Operators

==

==(Triple<A, B, C> other) -> bool

Compare two triples field by field.

The triples are equal when all three fields are equal, using the equality of the field types. A field type with two one-argument (==) overloads is compared with the one declared first, so == compiles only when that one takes the field type (lang.generics).

Parameters

other
The triple to compare with.

Returns

true if all three fields are equal.

Examples

use io::console;

Triple<int, int, int> a = Triple::Of(1, 2, 3);
console.writeln(a == Triple::Of(1, 2, 3));
console.writeln(a == Triple::Of(1, 2, 4));
console.writeln(a != Triple::Of(1, 2, 4));
true
false
true

See also

  • Pair — A pair of two values, possibly of different types.