Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Jacob Carlborg
On 2012-10-17 00:23, Jonathan M Davis wrote: 2. Or make it a range (see http://dlang.org/statement.html#foreach_with_ranges and http://ddili.org/ders/d.en/ranges.html ), which would probably be a bad idea, since containers really shouldn't be ranges. Why is that a bad idea? -- /Jacob

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Jonathan M Davis
On Wednesday, October 17, 2012 08:14:45 Jacob Carlborg wrote: On 2012-10-17 00:23, Jonathan M Davis wrote: 2. Or make it a range (see http://dlang.org/statement.html#foreach_with_ranges and http://ddili.org/ders/d.en/ranges.html ), which would probably be a bad idea, since containers

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Jacob Carlborg
On 2012-10-17 08:17, Jonathan M Davis wrote: For starters, iterating over the container would empty it. Right, but that is really weird, in my opinion. -- /Jacob Carlborg

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Jacob Carlborg
On 2012-10-17 17:45, Jonathan M Davis wrote: Well, what would you expect? Ranges are consumed when you iterate over them. So, if an container is a range, it will be consumed when you iterate over it. That's the way that it _has_ to work given how ranges work, and that's why you overload opSlice

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread H. S. Teoh
On Wed, Oct 17, 2012 at 06:58:52PM +0200, Jacob Carlborg wrote: On 2012-10-17 17:45, Jonathan M Davis wrote: Well, what would you expect? Ranges are consumed when you iterate over them. So, if an container is a range, it will be consumed when you iterate over it. That's the way that it

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Ali Çehreli
On 10/17/2012 09:58 AM, Jacob Carlborg wrote: On 2012-10-17 17:45, Jonathan M Davis wrote: Well, what would you expect? Ranges are consumed when you iterate over them. So, if an container is a range, it will be consumed when you iterate over it. That's the way that it _has_ to work given

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Jonathan M Davis
On Wednesday, October 17, 2012 10:08:15 H. S. Teoh wrote: On Wed, Oct 17, 2012 at 06:58:52PM +0200, Jacob Carlborg wrote: On 2012-10-17 17:45, Jonathan M Davis wrote: Well, what would you expect? Ranges are consumed when you iterate over them. So, if an container is a range, it will be

Re: How to define an interator to provide array like behaviour in a class?

2012-10-17 Thread Gary Willoughby
1. Define opApply (see section labeled Foreach over Structs and Classes with opApply after here: http://dlang.org/statement.html#foreach_with_ranges) 2. Or make it a range (see http://dlang.org/statement.html#foreach_with_ranges and http://ddili.org/ders/d.en/ranges.html ), which would

How to define an interator to provide array like behaviour in a class?

2012-10-16 Thread Gary Willoughby
I want to do something like this (Collection is a custom type): Collection x = new Collection(); x.add(something); x.add(somethingElse); foreach(type value; x) { writeln(value); } Collection is a class with a private array member variable which actually holds the collection data entered

Re: How to define an interator to provide array like behaviour in a class?

2012-10-16 Thread Jonathan M Davis
On Wednesday, October 17, 2012 00:03:46 Gary Willoughby wrote: I want to do something like this (Collection is a custom type): Collection x = new Collection(); x.add(something); x.add(somethingElse); foreach(type value; x) { writeln(value); } Collection is a class with a private