[Issue 3444] foreach(i, elem; range) should work

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 16 09:02:54 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=3444

Dmitry Olshansky <dmitry.olsh at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh at gmail.com

--- Comment #6 from Dmitry Olshansky <dmitry.olsh at gmail.com> ---
(In reply to Simen Kjaeraas from comment #5)
> Really? Cause this fails to compile for me:
> 
> unittest {
>     import std.range, std.stdio;
>     foreach (i, e; 100.iota)
>         writeln(i, ": ", e);
> }
> 
> Error: cannot infer argument types, expected 1 argument, not 2
> 
> Adding types for i and e, or using ranges other than iota, don't seem to
> change the result. This is on DMD 2.078.2 on Windows.
> 
> There's a workaround in std.range.enumerate. If that's the implementation
> you refer to, please include that information. If there's an implementation
> of this in an upcoming DMD release, please include that information.

This works today. And arguably there many other use cases that could work in
the same way, so I do doubt the niche case of 0..n indexing will be worth 
the enhancement to the compiler especially as it uses the same exact syntax as
tuple unpacking.


void main() {
    import std.range, std.stdio;
    foreach (i, e; 100.iota.enumerate)
        writeln(i, ": ", e);
}

--


More information about the Digitalmars-d-bugs mailing list