www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - foreach for self-defined data-structure?

reply Michiel <nomail please.com> writes:
I'm slowly working on a complete Set class. It would be good if I could
do this:

auto x = Set!(int);
foreach (element ; x) { ... }

Is there a way for me to define foreach for my own class? If not, maybe
there should be.

-- 
Michiel
Feb 21 2007
next sibling parent Michiel <nomail please.com> writes:
Michiel wrote:

 auto x = Set!(int);

Correction, I of course meant: auto x = new Set!(int); -- Michiel
Feb 21 2007
prev sibling parent reply BCS <BCS pathlink.com> writes:
Michiel wrote:
 I'm slowly working on a complete Set class. It would be good if I could
 do this:
 
 auto x = Set!(int);
 foreach (element ; x) { ... }
 
 Is there a way for me to define foreach for my own class? If not, maybe
 there should be.
 

look for opApply here http://www.digitalmars.com/d/statement.html#ForeachStatement
Feb 21 2007
parent Michiel <nomail please.com> writes:
BCS wrote:

 look for opApply here
 
 http://www.digitalmars.com/d/statement.html#ForeachStatement

How could I have missed that? :) Thanks! -- Michiel
Feb 21 2007