D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4577 - Third way to create a std.typecons.Tuple
Summary: Third way to create a std.typecons.Tuple
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: Andrei Alexandrescu
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2010-08-03 15:02 UTC by bearophile_hugs
Modified: 2016-11-15 18:59 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-08-03 15:02:23 UTC
Three ways to do something are a lot, but for me it's handy to build a std.typecons.tuple with just field names, and using the type inference to omit their types. With good library helper templates the implementation is simple:


import std.typecons: Tuple;
import std.string: split;
import std.metastrings: Format, toStringNow;

template SeriesGen1(string txt, string separator, int max, int min=0) {
    static if (min > max)
        const SeriesGen1 = "";
    else static if (min == max)
        const SeriesGen1 = Format!(txt, toStringNow!(max));
    else
        const SeriesGen1 = SeriesGen1!(txt, separator, max-1, min) ~ separator ~
                           Format!(txt, toStringNow!(max));
}

template SeriesGen2(string txt, string separator, int max, int min=0) {
    static if (min > max)
        const SeriesGen2 = "";
    else static if (min == max)
        const SeriesGen2 = Format!(txt, toStringNow!(max),
                                        toStringNow!(max));
    else
        const SeriesGen2 = SeriesGen2!(txt, separator, max-1, min) ~ separator ~
                           Format!(txt, toStringNow!(max),
                                        toStringNow!(max));
}

// not sure about the ref return value
// can this be named just "Tuple"?
ref auto Tuple_(string names, T...)(T args)
    if (T.length && split(names).length == T.length) {
    enum string[] fieldNames = split(names);

    mixin("return Tuple!(" ~
           SeriesGen2!("T[%s], fieldNames[%s]", ", ", T.length-1) ~
           ")(" ~
           SeriesGen1!("args[%s]", ", ", T.length-1) ~
           ");"
         );
}

// demo --------------------

import std.stdio: writeln;

ref auto sqr2(int a, float b) {
    return Tuple_!q{x y}(a * a, b * b);
}

void main() {
    ///*
    with(sqr2(10, 5.5F)) {
        writeln(x, " ", y);
    } // test.d(...): Error: sqr2(10,5.5F) is not an lvalue
    //*/

    auto xy = sqr2(10, 5.5F);
    with(xy) {
        writeln(x, " ", y);
    }

    with(Tuple!(int, "a", float, "b")(10, 5.5F)) {
        writeln(a, " ", b);
    }
}


I'd like this to overload the std.typecons.Tuple, I hope this is possible.

I am not sure if this enhancement request deserves a 'patch' keyword tag.
Comment 1 Nick Treleaven 2016-11-15 12:04:22 UTC
This now works:
auto entry = tuple!("index", "value")(4, "Hello");

http://dlang.org/phobos/typecons.html#tuple

Can we close this?