| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
April 08, 2015 Implementing Iterator to support foreach | ||||
|---|---|---|---|---|
| ||||
I am planning to implement "Iterator" class. But looking at "foreach" statement, it takes a range only.
So is there any way other than returning an array from a function that is to be passed foreach statement? So I could write like that:
Iterator iter = new MyList();
foreach(item; iter){
}
Otherwise I will need to write like that:
foreach(item; iter.getArrayOfAvailableItems()){
}
| ||||
April 08, 2015 Re: Implementing Iterator to support foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to tcak | On 04/07/2015 10:59 PM, tcak wrote: > I am planning to implement "Iterator" class. But looking at "foreach" > statement, it takes a range only. The other option is to overload opApply(): http://ddili.org/ders/d.en/foreach_opapply.html#ix_foreach_opapply.opApply Ali | |||
April 08, 2015 Re: Implementing Iterator to support foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to tcak | tcak:
> I am planning to implement "Iterator" class. But looking at "foreach" statement, it takes a range only.
Unless you are just experimenting, it's better to not go against a language and its std lib.
Bye,
bearophile
| |||
April 08, 2015 Re: Implementing Iterator to support foreach | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 8 April 2015 at 09:02:08 UTC, bearophile wrote:
> tcak:
>
>> I am planning to implement "Iterator" class. But looking at "foreach" statement, it takes a range only.
>
> Unless you are just experimenting, it's better to not go against a language and its std lib.
>
> Bye,
> bearophile
Also, why do you prefer an iterator over a range?
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply