[DGD] foreach

solicit solicit at hot.ee
Thu Sep 8 17:07:01 CEST 2005


I am currently trying to port a mudlib from MudOS to DGD, and I've hit a major show-stopper - foreach. Just so you know, I've read the mailing list archives threads about it.

I can agree that sometimes it is better and clearer to use for() instead of foreach() when iterating over an array, but no so for a mapping. Consider this example:

With foreach:
mapping m;
m["a"] = "foo";
m["b"] = "bar";
foreach(key, value in m) {
    printf("key: %s, value: %s\n", key, value);
}

Without foreach:
mapping m;
string key, value; /* (if the mapping is declared/filled in somewhere else, then it can be hard to guess the type [although you could use mixed]) */
string *keys;
int i;

m["a"] = "foo";
m["b"] = "bar";
keys = keys(m);
for(i = 0; i < sizeof(keys); i++) {
    key = keys[i];
    value = m[key];
    printf("key: %s, value: %s\n", key, value);
}

That's about twice as long (thrice if you don't count the filling-in-the-mapping part). Now consider me having to go through the 2300+ instances of foreach() in my mudlib (easily over half of them critical), and changing them all. Not something I'd like to do.

Now, if someone happens to have the old patch for DGD that adds foreach, then you can just ignore all this, but I thought I'd present my argument first. :)

-----------------------------------------
ITV - Sinu lemmiksaated internetis!
http://www.itv.ee




More information about the DGD mailing list