digitalmars.D - foreach with delegate as aggregate
- Russ Lewis (29/29) Sep 09 2004 An idea inspired by the "for over range" discussion:
An idea inspired by the "for over range" discussion: The following syntax makes sense to me. The idea is that you can pass a delegate into foreach as the collection type. The delegate is called to drive the foreach(), just like a class' opApply() would be called if an object was the collection type. The reason that this is useful is that you can build functions that iterate over generic collections (as the DoStuff() function below does) without the collections having to share some sort of common base type. void DoStuff(int delegate(int delegate(inout uint)) dg_apply) { foreach(uint u; dg_apply) { printf("%d\n", u); } } class Foo { uint array[]; int opApply(int delegate(inout uint) dg) { int result = 0; foreach(uint u; array) { result = dg(array[u]); if(result != 0) break; } return result; } } void test() { Foo f = <something>; DoStuff(f.opApply); }
Sep 09 2004