How to define an interator to provide array like behaviour in a class?

Gary Willoughby dev at kalekold.net
Tue Oct 16 15:03:46 PDT 2012


I want to do something like this (Collection is a custom type):

Collection x = new Collection();
x.add(something);
x.add(somethingElse);

foreach(type value; x)
{
     writeln(value);
}

Collection is a class with a private array member variable which 
actually holds the collection data entered via the add method. 
What do i need to add to the Collection class to make it iterable 
(whilst it's actually iterating through the member array) so the 
foreach loop works as expected?


More information about the Digitalmars-d-learn mailing list