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

1 auto t0 = tuple(4, "hello");
2 
3 auto t0Named = t0.rename!("val", "tag");
4 assert(t0Named.val == 4);
5 assert(t0Named.tag == "hello");
6 
7 Tuple!(float, "dat", size_t[2], "pos") t1;
8 t1.pos = [2, 1];
9 auto t1Named = t1.rename!"height";
10 t1Named.height = 3.4f;
11 assert(t1Named.height == 3.4f);
12 assert(t1Named.pos == [2, 1]);
13 t1Named.rename!"altitude".altitude = 5;
14 assert(t1Named.height == 5);
15 
16 Tuple!(int, "a", int, int, "c") t2;
17 t2 = tuple(3,4,5);
18 auto t2Named = t2.rename!("", "b");
19 // "a" no longer has a name
20 static assert(!__traits(hasMember, typeof(t2Named), "a"));
21 assert(t2Named[0] == 3);
22 assert(t2Named.b == 4);
23 assert(t2Named.c == 5);
24 
25 // not allowed to specify more names than the tuple has members
26 static assert(!__traits(compiles, t2.rename!("a","b","c","d")));
27 
28 // use it in a range pipeline
29 import std.range : iota, zip;
30 import std.algorithm.iteration : map, sum;
31 auto res = zip(iota(1, 4), iota(10, 13))
32     .map!(t => t.rename!("a", "b"))
33     .map!(t => t.a * t.b)
34     .sum;
35 assert(res == 68);
36 
37 const tup = Tuple!(int, "a", int, "b")(2, 3);
38 const renamed = tup.rename!("c", "d");
39 assert(renamed.c + renamed.d == 5);

Meta

Suggestion Box / Bug Report