tango.core.Tuple

The tuple module defines a template struct used for arbitrary data grouping.

License:

BSD style: see license.txt

Authors:

Walter Bright, Sean Kelly
template Tuple(TList...)
A Tuple is a an aggregate of typed values. Tuples are useful for returning a set of values from a function or for passing a set of parameters to a function.

NOTE:

Since the transition from user-defined to built-in tuples, the ability to return tuples from a function has been lost. Until this issue is addressed within the language, tuples must be enclosed in a struct if they are to be returned from a function.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
alias Tuple!(int, real) T1;
alias Tuple!(int, long) T2;
struct Wrap( Vals... )
{
    Vals val;
}

Wrap!(T2) func( T1 val )
{
    Wrap!(T2) ret;
    ret.val[0] = val[0];
    ret.val[1] = val[0] * cast(long) val[1];
    return ret;
}

This is the original tuple example, and demonstates what should be possible with tuples. Hopefully, language support will be added for this feature soon.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
alias Tuple!(int, real) T1;
alias Tuple!(int, long) T2;

T2 func( T1 val )
{
    T2 ret;
    ret[0] = val[0];
    ret[1] = val[0] * cast(long) val[1];
    return ret;
}


// tuples may be composed
alias Tuple!(int) IntTuple;
alias Tuple!(IntTuple, long) RetTuple;

// tuples are equivalent to a set of function parameters of the same type
RetTuple t = func( 1, 2.3 );
template IndexOf(T, TList...)
Returns the index of the first occurrence of T in TList or Tlist.length if not found.
template Remove(T, TList...)
Returns a Tuple with the first occurrence of T removed from TList.
template RemoveAll(T, TList...)
Returns a Tuple with all occurrences of T removed from TList.
template Replace(T, U, TList...)
Returns a Tuple with the first offuccrence of T replaced with U.
template ReplaceAll(T, U, TList...)
Returns a Tuple with all occurrences of T replaced with U.
template Reverse(TList...)
Returns a Tuple with the types from TList declared in reverse order.
template Unique(TList...)
Returns a Tuple with all duplicate types removed.
template MostDerived(T, TList...)
Returns the type from TList that is the most derived from T. If no such type is found then T will be returned.
template DerivedToFront(TList...)
Returns a Tuple with the types sorted so that the most derived types are ordered before the remaining types.