Tuples module

This module provides macros for declaring and decomposing tuples. A tuple is a group of values that may or may not be related that are stored in a single object. They can be useful for things like combining function parameters into a single named entity or returning multiple values from a function call.

A Tuple type can be declared with DeclTuple(...) or DeclNamedTuple(...) and referred to with Tuple(...) or the given name. Tuples can be easily decomposed with tuple_bind(self, ...) and scoped_tuple_bind(self, ...).

Example:

// declare a named tuple type called `Rectangle` that holds 4 `i32`s
DeclNamedTuple(Rectangle, i32, i32, i32, i32);

void example(Rectangle rect) {
    // bind the tuple's members to the given variable names, inside the enclosing scope
    tuple_bind(rect, x, y, width, height);
    println("x: {}, y: {}, width: {}, height: {}", x, y, width, height);
}

DeclTuple(f32, f32, f32, f32);

void example2(Tuple(f32, f32, f32, f32) params) {
    // bind the tuple's members to the given variable names, inside a new scope
    scoped_tuple_bind(params, first, second, third, fourth) {
        println("first: {}, second: {}, third: {}, fourth: {}", first, second, third, fourth);
    }
}

Defines

#define Tuple(...)
Used to refer to a tuple type.
#define DeclTuple(...)
Used to declare a tuple type.
#define DeclNamedTuple(...)
Used to declare a tuple type with a unique name The first argument must be the unique name to use for this tuple type. Subsequent arguments should be the types held by the tuple.
#define tuple_bind(self, ...)
Decomposes the given tuple self into one or more of its members, in declared order Decomposes self into one or more of its members, using the given names to refer to its members. The given names exist within the current scope.
#define scoped_tuple_bind(self, ...)
Decomposes the given tuple self into one or more of its members, in declared order Decomposes self into one or more of its members, using the given names to refer to its members. The given names are contained within a new scope.

Define documentation

#define Tuple(...)

Used to refer to a tuple type.

Parameters
... - The types contained by the tuple

#define DeclTuple(...)

Used to declare a tuple type.

Parameters
... - The types contained by the tuple

#define DeclNamedTuple(...)

Used to declare a tuple type with a unique name The first argument must be the unique name to use for this tuple type. Subsequent arguments should be the types held by the tuple.

Parameters
... - The first argument must be the unique name. Subsequent arguments should be the types contained by the tuple.

#define tuple_bind(self, ...)

Decomposes the given tuple self into one or more of its members, in declared order Decomposes self into one or more of its members, using the given names to refer to its members. The given names exist within the current scope.

Parameters
self - The tuple to decompose
... - The names to bind the tuple's members to

Example:

DeclNamedTuple(Point, i32, i32);

void example(Point point) {
    // binds the first member of point to `x`, the second to `y`
    // `x` and `y` exist in the current scope
    tuple_bind(point, x, y);
    println("Point: [x: {}, y: {}]", x, y);
}

#define scoped_tuple_bind(self, ...)

Decomposes the given tuple self into one or more of its members, in declared order Decomposes self into one or more of its members, using the given names to refer to its members. The given names are contained within a new scope.

Parameters
self - The tuple to decompose
... - The names to bind the tuple's members to

Example:

DeclNamedTuple(Point, i32, i32);

void example(Point point) {
    // binds the first member of point to `x`, the second to `y`
    scoped_tuple_bind(point, x, y) {
        // `x` and `y` only exist within this scope
        println("Point: [x: {}, y: {}]", x, y);
    }
    // `x` and `y` don't exist here
}