Array foreach in scripting languages
"Carsten Orthbandt" <[email protected]> Sat, 12 Aug 2006 15:46:24 +0200
| Newsgroups | gmane.games.devel.windows |
|---|---|
| Message-ID | <[email protected]> |
I'm currently building a small scripting language for interactive stuff. Now I
know there's plenty out there, I'm just doing this for fun.
A tricky question came up and I'd like to collect opinions and experiences on
this. It's about foreach for dynamic arrays. Consider this C++-like pseudo code:
DynamicArray<int> list;
int i;
for(i=0;i<20;i++){list.Add(i);};
foreach(i in list){
print(i);
if(i==8){list.Delete(12);};
};
The obvious question is how should an active iterator react if the traversed list
changes? To answer this, I simply tried it out in two languages I know that do
provide both dynamic arrays and foreach: STL, Tcl and C#.
In STL C++ code, I get an exception because of iterator incompatibility.
In Tcl this problem isn't one because the iterator takes a snapshot of the list
object on start and there's no language construct to actually change this instance.
In C# with the List<int> generic, I get an exception because the iterated object
changes (interestingly, this happens on ANY change to the list, even appending).
Now what I'd like to know:
- how do other languages cope with this? (Tcl doesn't need to, C++/C# simply fails)
- what do YOU think would be sensible behaviour?
Currently I'm considering having all iterators be elements of a double-linked list
and let the array object be the dl-list head. So if the number of array elements
changes, it can run through all attached iterators and fix their indices or stop them.
Carsten Orthbandt
Founder + Development Director
SEK SpieleEntwicklungsKombinat GmbH
http://www.sek-ost.de
Wenn ich Visionen habe, gehe ich zum Arzt. - Helmut Schmidt
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555