Bug 4579 – std.typecons.Tuple syntax unpacking sugar

Status
RESOLVED
Resolution
LATER
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-08-03T17:13:16Z
Last change time
2018-05-18T07:42:35Z
Assigned to
Andrei Alexandrescu
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-08-03T17:13:16Z
In this enhancement request I propose to add syntax sugar to D2 that allows to "unpack" std.typecons.Tuple at the calling point. This enhancement request doesn't ask for a third new kind of tuple, the normal std.typecons.Tuple Phobos2 one is enough. Essentially std.typecons.Tuple can be used to have multiple return values in D, but some syntax sugar helps. In Python2.x language, that is able to return tuples too, this automatic tuple unpacking is _very_ common and very handy (D makes this situation a little different, because std.typecons.Tuple supports named fields, and fields are statically typed): def sqr_cube(x): return x*x, x*x*x x2, x3 = sqr_cube(10) (Python2.x also allows nested tuple unpacking with a bit of pattern matching, and Python3.x allows "variable length" unpacking with the * syntax.) This is a function of std.file (Phobos2 of DMD 2.047): void getTimes(in char[] name, out d_time ftc, out d_time fta, out d_time ftm); Introducing tuples more into Phobos its signature becomes: Tuple!(d_time "ftc", d_time "fta", d_time "ftm") getTimes(const string name); But then you have to use it for example like this: void main() { auto t3 = getTimes("filename"); with(t3) { writeln(ftc, " ", fta, " ", ftm); } } While adding a bit of syntax sugar to D it becomes: void main() { // Here I have used "fc" != "ftc" (d_time fc, d_time fa, d_time fm) = getTimes("filename"); } That syntax sugar means something like: void main() { auto __temp1 = getTimes("filename"); d_time fc = __temp1.field[0]; d_time fa = __temp1.field[1]; d_time fm = __temp1.field[2]; } I think that sugar doesn't clash with C syntax, so this syntax doesn't introduce bugs when porting code from C to D. This new syntax looks natural enough to me, and handy in high-level-style D programming. Nested unpacking too is a possibility: (int x, (int y, int z)) = foo(); See also bug 4577 for Tuples.
Comment #1 by bearophile_hugs — 2010-09-16T19:24:57Z
'auto' too may be supported: void main() { (auto fc, auto fa, auto fm) = getTimes("filename"); } Or even: void main() { auto (fc, fa, fm) = getTimes("filename"); } An unpacking syntax for Tuples is useful to replace the very similar zip() and lockstep(): import std.algorithm, std.stdio, std.range; void main() { foreach (p; zip([1, 2, 3], "abcd")) writeln(p._0, " ", p.length, " ", p._1); writeln(); foreach (i, a, b; lockstep([1, 2, 3], "abcd")) writeln(i, " ", a, " ", b); } with a single zip() that may be used for both situations: import std.algorithm, std.stdio, std.range; void main() { foreach (p; zip([1, 2, 3], "abcd")) writeln(p[0], " ", p[1]); writeln(); foreach ((a, b); zip([1, 2, 3], "abcd")) writeln(a, " ", b); } As in Python: for p in zip([1, 2, 3], "abcd"): print p[0], p[1] print for (a, b) in zip([1, 2, 3], "abcd"): print a, b (The zip() is a very commonly useful higher order function.)
Comment #2 by bearophile_hugs — 2011-11-18T18:13:42Z
See also issue 6365
Comment #3 by bearophile_hugs — 2012-02-29T14:10:39Z
This syntax can't be used, it's part of the new lambda syntax: auto tups = [tuple(1,2), tuple(3,4)]; auto r = map!((x,y) => x * y)(tups); A possible solution: auto tups = [tuple(1,2), tuple(3,4)]; auto r = map!(tuple(x,y) => x * y)(tups);
Comment #4 by dmitry.olsh — 2018-05-18T07:42:35Z
There is an onging Tuple DIP that is actually solid: https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md