On Monday, 5 February 2024 at 21:12:58 UTC, Gary Chike wrote:
[ ... ]

In addition to the methods hitherto provided:

```d
auto getUser() => tuple("John Doe", 32);

// Name a Tuple's fields post hoc by copying the original's fields into a new Tuple.
template named(names...) {
auto named(T)(ref auto T t) if (names.length <= T.Types.length) =>
        tuple!names(t.expand[0..names.length]);
}

{
    auto u = getUser().named!("name", "age");
    writeln(u.name, " (", u.age, ")"); // John Doe (32)

    // You could also do this. If only `with` were an expression!
    with (getUser().named!("name", "age"))
        writeln(name, " (", age, ")"); // John Doe (32)
}

// Define variables corresponding to a Tuple's fields in the current scope, whose
// identifiers are given by `names`.
mixin template Unpack(alias t, names...)
if (names.length <= t.Types.length) {
    static foreach (i, n; names)
        mixin("auto ", n, " = t[i];");
}

{
    auto u = getUser();
    mixin Unpack!(u, "name", "age");
    writeln(name, " (", age, ")"); // John Doe (32)
}
```

For either of these, one can "unpack" some or none of a Tuple's fields, and both can be modified to ignore any field to simulate `val (name, _, prof) = ("John Doe", 32, "Chemist")`.

Reply via email to