Overriding iteration
Simen kjaeraas
simen.kjaras at gmail.com
Fri Mar 4 08:46:39 PST 2011
Simen kjaeraas <simen.kjaras at gmail.com> wrote:
> Magnus Lie Hetland <magnus at hetland.org> wrote:
>
>> From what I understand, when you override iteration, you can either
>> implement the basic range primitives, permitting foreach to
>> destructively iterate over your object, or you can implement a custom
>> method that's called, and that must perform the iteration. The
>> destructiveness of the first option can, of course, be mitigated if you
>> use a struct rather than a class, and make sure that anything that
>> would be destroyed by popFront() is copied.
>>
>> What I'm wondering is whether there is a way to do what Python does --
>> to construct/return an iterator (or, in this case, a range) that is
>> used during the iteration, rather than the object itself?
>>
>> I'm thinking about when you iterate directly over the object here. As
>> far as I can see, the solution used in the std.container is to use
>> opSlice() for this functionality. In other words, in order to iterate
>> over container foo, you need to use foreach(e; foo[])? Is there no way
>> to get this functionality directly (i.e., for foreach(e; foo))?
>
> foreach ( e; foo ) {}
> Should work. I believe there is a bug already filed on it not working.
Found it:
http://d.puremagic.com/issues/show_bug.cgi?id=5605
--
Simen
More information about the Digitalmars-d-learn
mailing list