D - foreach using namespaces
- Vathix (19/19) Dec 02 2003 Using a namespace we could have separate foreach's:
- J Anderson (26/46) Dec 04 2003 Of course you can do this now:
Using a namespace we could have separate foreach's:
class Foo
{
int opApply(int delegate(inout int) dg);
namespace reverse
{
int opApply(int delegate(inout int) dg);
}
}
int main()
{
Foo foo = new Foo;
foreach(int i; foo) { }
foreach(int i; foo.reverse) { }
return 0;
}
That would obviously go over the elements in reverse order. There could also
be others: foreach depending on certain attributes such as for a file list,
a directory namespace with opApply that only goes over the directories.
Dec 02 2003
Vathix wrote:
Using a namespace we could have separate foreach's:
class Foo
{
int opApply(int delegate(inout int) dg);
namespace reverse
{
int opApply(int delegate(inout int) dg);
}
}
int main()
{
Foo foo = new Foo;
foreach(int i; foo) { }
foreach(int i; foo.reverse) { }
return 0;
}
That would obviously go over the elements in reverse order. There could also
be others: foreach depending on certain attributes such as for a file list,
a directory namespace with opApply that only goes over the directories.
Of course you can do this now:
class FooR
{
this(Foo other) { Other = other; }
int opApply(int delegate(inout int) dg) { return 0; }
Foo Other;
}
class Foo
{
int opApply(int delegate(inout int) dg) { return 0; }
FooR reverse() { return new FooR(this); }
}
int main( char [] [] args )
{
Foo foo = new Foo;
foreach(int i; foo) { }
foreach(int i; foo.reverse()) { }
return 1;
}
But it's not quite as concise or efficient. If this syntax sugar was to
be brought in, It wouldn't be limit it to opApply opperators. Parhaps
it's a way to encourage opperator reuse in opperator overloading.
foo.w ~ foo2.w ~ foo3.w;
foo.r ~ foo2.r ~ foo3.r;
-Anderson
Dec 04 2003








J Anderson <REMOVEanderson badmama.com.au>