How to iterate using foreach on a class?

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Nov 1 08:32:35 PDT 2013


01-Nov-2013 16:43, John Colvin пишет:
> On Friday, 1 November 2013 at 12:37:20 UTC, simendsjo wrote:
>> 2) opSlice
>> 3) alias this
>
> arguably these are the same in the context of the foreach loop. Both
> just provide direct access to the underlying array.

No quite. I'd say alias this is frankly a bad idea to provide iteration.
In any case returning naked underlying array is most surely short-sighted.

In short we have 2 ways:
1) Ranges
2) opApply

And a couple of extra things on _top_ of that to keep in mind:
1) Implicit conversion -> hence alias this to a range/opApply type works
2) opSlice is called on foreach aggregate if it's not a range or doesn't 
have opApply by itself.

-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list