Bug 7361 – No documentation for front tuple expansion in foreach over range
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-24T06:42:36Z
Last change time
2024-05-16T11:11:46Z
Keywords
bootcamp
Assigned to
No Owner
Creator
Denis Shelomovskii
Comments
Comment #0 by verylonglogin.reg — 2012-01-24T06:42:36Z
As kenji hara wrote in NG:
Today, foreach can expand the front tuple automatically.
foreach (i, e; zip(sequence!"n", range))
{
// i = 0, 1, 2, ...
// e = elements of range
}
So extra unpacking syntax is not need.
Kenji Hara
Comment #1 by bearophile_hugs — 2013-03-26T06:05:28Z
In Issue 9817 I have suggested to remove that syntax, and replace it with something more explicit and more flexible.
Comment #2 by bearophile_hugs — 2014-08-31T07:34:07Z
So I think it's better to not document this anti-feature.
Comment #3 by vlevenfeld — 2014-08-31T09:11:55Z
I'm not sure its such an anti-feature - I keep having "cannot infer argument types" bugs using foreach on custom ranges, and zipping them with an enumeration sequence beforehand seems a pleasant nice enough workaround.
(To be fair this may be fixed in 2.067 but I don't know because 2.067 segfaults on my codebase and I haven't had time to hunt down the cause yet.)
In any case I feel it should either be documented or removed because it caught me by surprise and took a little bit of time to figure out what was going on, mainly because the lack of documentation.
Comment #4 by vlevenfeld — 2014-08-31T09:12:58Z
sorry I meant 2.066
Comment #5 by nick — 2016-03-13T13:17:10Z
While it's not documented in the spec, this feature is already documented in an example in std.range.enumerate:
http://dlang.org/phobos/std_range.html#.enumerate
foreach (lineNum, line; stdin.byLine().enumerate(1))
stdout.writefln("line #%s: %s", lineNum, line);