January 07, 2012 Re: writing iterators without code duplication. inout? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pompei2 | On Wed, 21 Dec 2011 11:34:18 -0500, pompei2 <pompei2@gmail.com> 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 iteration, don't know if that's what you want in general though. > > 1&3: Because there are different things in my class to iterate over. Think foreach(p ; obj.properties()) and foreach(c ; obj.components()). (I know, I can make those @property so I don't need the ().) > > 2. Because if not, it says: Error: cannot implicitly convert expression (__foreachbody1315) of type int delegate(ref int) to int delegate(int) Two of my most wished for bugs: http://d.puremagic.com/issues/show_bug.cgi?id=2443 http://d.puremagic.com/issues/show_bug.cgi?id=2498 Looks like 2443 has been fixed in head! -Steve |
January 07, 2012 Re: writing iterators without code duplication. inout? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pompei2 | On Wed, 21 Dec 2011 10:54:06 -0500, pompei2 <pompei2@gmail.com> 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'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 function.
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.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation