Tuple.rename

Renames the elements of a Tuple.

rename uses the passed names and returns a new Tuple using these names, with the content unchanged. If fewer names are passed than there are members of the Tuple then those trailing members are unchanged. An empty string will remove the name for that member. It is an compile-time error to pass more names than there are members of the Tuple.

  1. ref rename()
    struct Tuple
    ref inout return
    rename
    (
    names...
    )
    ()
    if (
    names.length == 0 ||
    )
  2. ref rename()

Examples

auto t0 = tuple(4, "hello");

auto t0Named = t0.rename!("val", "tag");
assert(t0Named.val == 4);
assert(t0Named.tag == "hello");

Tuple!(float, "dat", size_t[2], "pos") t1;
t1.pos = [2, 1];
auto t1Named = t1.rename!"height";
t1Named.height = 3.4f;
assert(t1Named.height == 3.4f);
assert(t1Named.pos == [2, 1]);
t1Named.rename!"altitude".altitude = 5;
assert(t1Named.height == 5);

Tuple!(int, "a", int, int, "c") t2;
t2 = tuple(3,4,5);
auto t2Named = t2.rename!("", "b");
// "a" no longer has a name
static assert(!__traits(hasMember, typeof(t2Named), "a"));
assert(t2Named[0] == 3);
assert(t2Named.b == 4);
assert(t2Named.c == 5);

// not allowed to specify more names than the tuple has members
static assert(!__traits(compiles, t2.rename!("a","b","c","d")));

// use it in a range pipeline
import std.range : iota, zip;
import std.algorithm.iteration : map, sum;
auto res = zip(iota(1, 4), iota(10, 13))
    .map!(t => t.rename!("a", "b"))
    .map!(t => t.a * t.b)
    .sum;
assert(res == 68);

const tup = Tuple!(int, "a", int, "b")(2, 3);
const renamed = tup.rename!("c", "d");
assert(renamed.c + renamed.d == 5);

Meta