[Issue 5550] std.range.enumerate()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 17 01:34:22 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=5550



--- Comment #13 from Jakob Ovrum <jakobovrum at gmail.com> 2014-01-17 01:34:19 PST ---
(In reply to comment #12)
> This should go in the unittests of enumerate():
> 
> 
> void main() {
>     auto arr = [0];
>     foreach (immutable i, ref x; arr.enumerate)
>         x++;
>     assert(arr[0] == 1);
>     foreach (immutable i, ref x; arr.enumerate(10))
>         x++;        
>     assert(arr[0] == 2);
> }
>

As far as I can tell, this cannot be implemented; it is an error that the
compiler accepts use of `ref` in foreach over a range that doesn't have
assignable elements. It is a problem shared with many other higher order
ranges.

> And in the "documentation unittests" of enumerate I think it's a good idea to
> add (I explain this use case in the #Comment4):
> 
> void main() {
>     import std.stdio, std.range;
> 
>     auto M = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
>     foreach (immutable r, row; M)
>         foreach (immutable c, ref item; row.enumerate[1 .. $])
>             item = c * 10 + r;
>     writefln("%(%s\n%)", M);
> }
> 
> 
> With its output:
> 
> [0, 10, 20, 30]
> [0, 11, 21, 31]
> [0, 12, 22, 32]

It's a good example, but I think it looks a little bit ugly without `ref`. I
would also change [1 .. $] to .drop(1), but it is a minor stylistic issue.

Also, to be a documented unittest, it needs to use assertions instead of pipe
I/O.

I still recommend you post on Github, it's likely that it will get more review
and comments there. Also, it's only right that implementation comments go to
the implementation proposed on Github, with the issue tracker dedicated to the
problem statement in general...

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


More information about the Digitalmars-d-bugs mailing list