Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread Las via Digitalmars-d
On Thursday, 26 January 2017 at 11:32:09 UTC, ZombineDev wrote: Anyway, another solution is to use refRange: void main() { import std.range : refRange; S s; auto p = refRange(); foreach(i; p) writeln(i); } That way you don't need to dereference 'p' everytime you want

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread ZombineDev via Digitalmars-d
On Thursday, 26 January 2017 at 12:03:39 UTC, guest wrote: On Thursday, 26 January 2017 at 11:32:09 UTC, ZombineDev wrote: Not sure if this is a bug in isInputRange or foreach, but they should work consistently. Copy/paste from primitives.d: template isInputRange(R) { enum bool

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread guest via Digitalmars-d
On Thursday, 26 January 2017 at 11:32:09 UTC, ZombineDev wrote: Not sure if this is a bug in isInputRange or foreach, but they should work consistently. Copy/paste from primitives.d: template isInputRange(R) { enum bool isInputRange = is(typeof( (inout int = 0) { R r =

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread ZombineDev via Digitalmars-d
On Wednesday, 25 January 2017 at 16:15:49 UTC, Las wrote: So the reference says that (when range has the properties) `foreach (e; range) { ... }` is equivalent to: for (auto __r = range; !__r.empty; __r.popFront()) { auto e = __r.front; ... } Though this isn't always true, as when I

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread Jonathan M Davis via Digitalmars-d
On Thursday, January 26, 2017 09:05:17 Las via Digitalmars-d wrote: > On Wednesday, 25 January 2017 at 20:22:54 UTC, Jonathan M Davis > > wrote: > > It only matters if you're trying to define the range primitives > > for your range as free functions for some reason. Just put them > > on the type

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread guest via Digitalmars-d
On Wednesday, 25 January 2017 at 16:15:49 UTC, Las wrote: then I can do this: void main() { S s; auto p = p.popFront; writeln(p.front); } But not this: void main() { S s; auto p = foreach(i; p) writeln(i); } p.popFront

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-26 Thread Las via Digitalmars-d
On Wednesday, 25 January 2017 at 20:22:54 UTC, Jonathan M Davis wrote: It only matters if you're trying to define the range primitives for your range as free functions for some reason. Just put them on the type itself and be done with it. The only reason that wouldn't work would be if you

Re: foreach over pointers OR foreach that mutates the iterator

2017-01-25 Thread Jonathan M Davis via Digitalmars-d
On Wednesday, January 25, 2017 16:15:49 Las via Digitalmars-d wrote: > So the reference says that (when range has the properties) > `foreach (e; range) { ... }` is equivalent to: > for (auto __r = range; !__r.empty; __r.popFront()) > { > auto e = __r.front; > ... > } > > Though this

foreach over pointers OR foreach that mutates the iterator

2017-01-25 Thread Las via Digitalmars-d
So the reference says that (when range has the properties) `foreach (e; range) { ... }` is equivalent to: for (auto __r = range; !__r.empty; __r.popFront()) { auto e = __r.front; ... } Though this isn't always true, as when I use this struct: struct S { int front = 10;