Thread overview
[Issue 10119] New: Add tuple overload which automatically captures the names of symbols
May 19, 2013
Andrej Mitrovic
May 19, 2013
Jakob Ovrum
May 19, 2013
Andrej Mitrovic
Sep 19, 2013
Andrej Mitrovic
Oct 20, 2013
Andrej Mitrovic
May 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119

           Summary: Add tuple overload which automatically captures the
                    names of symbols
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-05-19 11:41:16 PDT ---
Currently tuple() can capture the state of local variables and wrap them in a Tuple struct. However you won't have name access:

-----
import std.typecons;
auto foo()
{
    int x, y;
    return tuple(x, y);
}

void main()
{
    assert(foo.x == 0);  // fails, no "x"
}
-----

As a workaround you can explicitly name the fields at the call site:

-----
auto foo()
{
    int x, y;
    return Tuple!(typeof(x), "x", typeof(y), "y")(x, y);
}
-----

But this is tedious and boring, and we can do much better than this. I propose we introduce either an overload of tuple (if it's possible) or a newly named function which automatically wraps the arguments by name into a tuple.

Here's an example implementation:

-----
import std.string;
import std.typecons;

auto tuplify(Aliases...)()
{
    string gen()
    {
        string[] lhs;
        string[] rhs;

        foreach (idx; 0 .. Aliases.length)
        {
            lhs ~= format("typeof(Aliases[%s])", idx);
            lhs ~= format("__traits(identifier, Aliases[%s])", idx);
            rhs ~= format("Aliases[%s]", idx);
        }

        return format("Tuple!(%s)(%s)", lhs.join(", "), rhs.join(", "));
    }

    return mixin(gen());
}

auto func()
{
    int x = 1;
    string y = "2";
    return tuplify!(x, y);
}

void main()
{
    auto res = func;
    assert(res.x == 1);
    assert(res.y == "2");
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119


Jakob Ovrum <jakobovrum@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakobovrum@gmail.com


--- Comment #1 from Jakob Ovrum <jakobovrum@gmail.com> 2013-05-19 11:55:15 PDT ---
No, this instead. (per IRC request)

https://gist.github.com/JakobOvrum/5608585
------------------------------------------
import std.typecons : Tuple;
import std.typetuple : TypeTuple;

template NameTypePairs(alias front, vars...)
{
    private enum name = __traits(identifier, front);
    private alias pair = TypeTuple!(typeof(front), name);

    static if(vars.length == 0)
        alias NameTypePairs = pair;
    else
        alias NameTypePairs =  TypeTuple!(pair, NameTypePairs!vars);
}

auto tuplify(vars...)()
{
    return Tuple!(NameTypePairs!vars)(vars);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119



--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-05-19 12:00:50 PDT ---
(In reply to comment #1)
> No, this instead. (per IRC request)

That one is also faster and more generic-style instead of magic-mixin-style. *Nods head in approval*.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #3 from bearophile_hugs@eml.cc 2013-05-19 16:10:45 PDT ---
I think we should just add tuples to D. I think a possible syntax is
tuple(...).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 19, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-18 17:53:05 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1585

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10119


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------