Re: writing iterators without code duplication. inout?

2012-01-07 Thread Steven Schveighoffer
On Wed, 21 Dec 2011 10:54:06 -0500, pompei2 wrote: Hello. I want to add the option to iterate objects of my class using foreach. I need them to be iterable as view-only const and as mutable too. I would prefer to iterate using the "return a delegate" but if that's not possible, ranges ar

Re: writing iterators without code duplication. inout?

2012-01-07 Thread Steven Schveighoffer
On Wed, 21 Dec 2011 11:34:18 -0500, pompei2 wrote: On Wednesday, 21 December 2011 at 16:05:24 UTC, Trass3r wrote: Can't really answer your original question, but 1. Why don't you use opApply? 2. Why do you use ref int even in the const version? 3. You could also use alias this to allow iterati

Re: writing iterators without code duplication. inout?

2011-12-22 Thread Andrew Wiley
On Thu, Dec 22, 2011 at 12:04 AM, pompei2 wrote: >> int delegate(int delegate(ref int)) doIter() const >>  { >> 74      return (int delegate(ref int) dg) >>      { >>        cast(typeof(this))(this).doIter() >> 77            ( >> 78              (ref int i) >> >>              { >>                i

Re: writing iterators without code duplication. inout?

2011-12-22 Thread pompei2
int delegate(int delegate(ref int)) doIter() const { 74 return (int delegate(ref int) dg) { cast(typeof(this))(this).doIter() 77( 78 (ref int i) { int copy = i; dg(copy); } ); } } I see w

Re: writing iterators without code duplication. inout?

2011-12-21 Thread Christophe
"pompei2" , dans le message (digitalmars.D.learn:31164), a écrit : > This is what I have, which works but has severe code duplication. > I hoped inout would help me here, but I just can't figure it out. > I also gave a try to ranges, but same thing again: I can only get > it to work if I define

Re: writing iterators without code duplication. inout?

2011-12-21 Thread Jakob Ovrum
On Wednesday, 21 December 2011 at 16:07:55 UTC, Timon Gehr wrote: Just remove the non-const overload. const member functions work with mutable, immutable and const receivers. The const version does not allow using 'ref' when iterating.

Re: writing iterators without code duplication. inout?

2011-12-21 Thread pompei2
On Wednesday, 21 December 2011 at 16:05:24 UTC, Trass3r wrote: Can't really answer your original question, but 1. Why don't you use opApply? 2. Why do you use ref int even in the const version? 3. You could also use alias this to allow iteration, don't know if that's what you want in general tho

Re: writing iterators without code duplication. inout?

2011-12-21 Thread Jakob Ovrum
On Wednesday, 21 December 2011 at 16:31:01 UTC, Jakob Ovrum wrote: On Wednesday, 21 December 2011 at 16:07:55 UTC, Timon Gehr wrote: Just remove the non-const overload. const member functions work with mutable, immutable and const receivers. The const version does not allow using 'ref' when it

Re: writing iterators without code duplication. inout?

2011-12-21 Thread pompei2
Just remove the non-const overload. const member functions work with mutable, immutable and const receivers. Doing this, it compiles but it doesn't do what it should in two ways: 1. I leave the code as-is. It compiles but "e" in the first foreach loop in "main" is a copy, not a reference.

Re: writing iterators without code duplication. inout?

2011-12-21 Thread Timon Gehr
On 12/21/2011 04:54 PM, pompei2 wrote: Hello. I want to add the option to iterate objects of my class using foreach. I need them to be iterable as view-only const and as mutable too. I would prefer to iterate using the "return a delegate" but if that's not possible, ranges are fine too. Also, I'

Re: writing iterators without code duplication. inout?

2011-12-21 Thread Trass3r
Can't really answer your original question, but 1. Why don't you use opApply? 2. Why do you use ref int even in the const version? 3. You could also use alias this to allow iteration, don't know if that's what you want in general though.

writing iterators without code duplication. inout?

2011-12-21 Thread pompei2
Hello. I want to add the option to iterate objects of my class using foreach. I need them to be iterable as view-only const and as mutable too. I would prefer to iterate using the "return a delegate" but if that's not possible, ranges are fine too. Also, I'd prefer a template-less solution ov