www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implementing Iterator to support foreach

reply "tcak" <tcak gmail.com> writes:
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()){
}
Apr 07 2015
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Apr 07 2015
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Apr 08 2015
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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?
Apr 08 2015