Re: Unexpected behaviour of foreach statement

2018-03-02 Thread Arredondo via Digitalmars-d-learn
On Friday, 2 March 2018 at 10:32:08 UTC, Jonathan M Davis wrote: foreach does not support indices for ranges, only arrays. When you have foreach(e; range) it gets lowered to foreach(auto __range = range; !__range.empty; __range.popFront()) { auto e = __range.front; } There are no

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread Arredondo via Digitalmars-d-learn
On Friday, 2 March 2018 at 10:34:31 UTC, bauss wrote: You can also call "array" from "std.array". auto range = iota(5).array; foreach (i, el; range) { writeln(i, ": ", el); } Thank you. That's how I had it in my original code, I was just trying to avoid gratuitous memory

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread Arredondo via Digitalmars-d-learn
On Friday, 2 March 2018 at 10:27:27 UTC, Nicholas Wilson wrote: try https://dlang.org/phobos/std_range.html#enumerate This worked. Thank you!

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread bauss via Digitalmars-d-learn
On Friday, 2 March 2018 at 10:21:39 UTC, Arredondo wrote: Hi, The following works as expected: auto range = [1, 2, 3, 4, 5]; foreach (i, el; range) { writeln(i, ": ", el); } but this slight modification doesn't: auto range = iota(5); foreach (i, el; range) { writeln(i, ": ",

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, March 02, 2018 10:21:39 Arredondo via Digitalmars-d-learn wrote: > Hi, > > The following works as expected: > > auto range = [1, 2, 3, 4, 5]; > foreach (i, el; range) { > writeln(i, ": ", el); > } > > but this slight modification doesn't: > > auto range = iota(5); > foreach (i, el;

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 2 March 2018 at 10:21:39 UTC, Arredondo wrote: Hi, The following works as expected: auto range = [1, 2, 3, 4, 5]; foreach (i, el; range) { writeln(i, ": ", el); } but this slight modification doesn't: auto range = iota(5); foreach (i, el; range) { writeln(i, ": ",

Unexpected behaviour of foreach statement

2018-03-02 Thread Arredondo via Digitalmars-d-learn
Hi, The following works as expected: auto range = [1, 2, 3, 4, 5]; foreach (i, el; range) { writeln(i, ": ", el); } but this slight modification doesn't: auto range = iota(5); foreach (i, el; range) { writeln(i, ": ", el); } DMD 2.078.3 says: Error: cannot infer argument

Re: Unexpected behaviour of foreach statement

2018-03-02 Thread rikki cattermole via Digitalmars-d-learn
On 02/03/2018 11:21 PM, Arredondo wrote: Hi, The following works as expected: auto range = [1, 2, 3, 4, 5]; foreach (i, el; range) { writeln(i, ": ", el); } s/range/array/ Arrays have a different foreach syntax than ranges do.