writing iterators without code duplication. inout?
schveiguy at yahoo.com
Sat Jan 7 08:49:52 PST 2012
On Wed, 21 Dec 2011 10:54:06 -0500, pompei2 <pompei2 at gmail.com> wrote:
> 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
> over a templated one.
> 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 my things twice.
inout cannot be used here, because inout is const within an inout
function. This means you cannot modify data while in the context of the
function, and the foreach delegate is called within the context of the
Having dealt with foreach in my container lib, I can tell you, it's not
ideal. I'm hoping to have some way to do tail-const ranges in the future,
which should help with code duplication.
More information about the Digitalmars-d-learn