digitalmars.D.bugs - [Issue 4579] New: std.typecons.Tuple syntax unpacking sugar
- d-bugmail puremagic.com (60/60) Aug 03 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4579
- d-bugmail puremagic.com (39/39) Sep 16 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4579
- d-bugmail puremagic.com (10/10) Jan 09 2011 http://d.puremagic.com/issues/show_bug.cgi?id=4579
- d-bugmail puremagic.com (6/6) Nov 18 2011 http://d.puremagic.com/issues/show_bug.cgi?id=4579
- d-bugmail puremagic.com (11/11) Feb 29 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4579
http://d.puremagic.com/issues/show_bug.cgi?id=4579 Summary: std.typecons.Tuple syntax unpacking sugar Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: bearophile_hugs eml.cc 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 03 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4579 '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.) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4579 Andrei Alexandrescu <andrei metalanguage.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |ASSIGNED CC| |andrei metalanguage.com AssignedTo|nobody puremagic.com |andrei metalanguage.com -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 09 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4579 See also issue 6365 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 18 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4579 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); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 29 2012