[GD-Windows] Array foreach in scripting languages
Brought to you by:
vexxed72
From: Carsten O. <car...@se...> - 2006-08-12 13:46:14
|
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. =20 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: =20 DynamicArray<int> list; int i; for(i=3D0;i<20;i++){list.Add(i);}; foreach(i in list){ print(i); if(i=3D=3D8){list.Delete(12);}; }; =20 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#. =20 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). =20 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? =20 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. =20 =20 Carsten Orthbandt Founder + Development Director SEK SpieleEntwicklungsKombinat GmbH http://www.sek-ost.de Wenn ich Visionen habe, gehe ich zum Arzt. - Helmut Schmidt =20 =20 |