Mark Sibly 9 gadi atpakaļ
vecāks
revīzija
d656dde82a

+ 74 - 1
modules/std/collections/container.monkey2

@@ -1,7 +1,80 @@
 
 Namespace std.collections
 
-#rem monkeydoc IContainer interface.
+#rem monkeydoc The IContainer interface is a 'dummy' interface that container classes should implement for compatibility with [[Eachin]] loops.
+
+IContainer does not actually declare any members, but a class that implements IContainer should implement the follow method:
+
+`Method All:IteratorType()` - Gets an iterator to all values in the container.
+
+...where 'IteratorType' is a class or struct type that implements the following properties and methods:
+
+`Property Current:ValueType()` - The current value pointed to by the iterator.
+
+`Property AtEnd:bool()` - true if iterator is at end of container.
+
+`Method Erase()` - Erases the value pointed to by the iterator.
+
+`Method Bump()` - Bumps the iterator so it points to the next value in the container.
+
+...where 'ValueType' is the type of the values contained in the container.
+
+With these conditions met, a container can be used with eachin loops. Monkey2 will automatically convert code like this:
+
+```
+For Local value:=Eachin container
+
+	...loop code here...
+
+Next
+```
+
+...to this...
+
+```
+Local iterator:=container.All()
+
+While Not iterator.AtEnd
+
+	Local value:=iterator.Current
+	
+	...loop code here...
+	
+	iterator.Bump()
+Wend
+```
+
+Containers should not be modified while eachin is being used to loop through the values in the container as this can put the container into
+an inconsistent state. If you need to do this, you should manually iterate through the container and use the iterator 'Erase' method to erase
+values in a controlled way. For example:
+
+```
+Local iterator:=container.All()
+
+While Not iterator.AtEnd
+
+	Local value:=iterator.Current
+
+	Local eraseMe:=false
+
+    ...loop code here - may set eraseMe to true to erase current value...
+
+	If eraseMe
+
+		iterator.Erase()
+
+	Else
+
+		iterator.Bump()
+	Endif
+Wend
+```
+
+Note that if you erase a value, you should NOT bump the iterator - erase implicitly does this for you.
+
+Finally, IContainer is not a 'real' interface because Monkey2 does not yet support generic interface methods. This feature is planned for a 
+future version of monkey2.
+
 #end
 Interface IContainer<T>
 

+ 6 - 1
modules/std/collections/list.monkey2

@@ -5,7 +5,12 @@ Alias IntList:List<Int>
 Alias FloatList:List<Float>
 Alias StringList:List<String>
 
-#rem monkeydoc The List class.
+#rem monkeydoc The list class provides support for doubly linked lists.
+
+Lists implements the [[IContainer]] interface so can be used with [[Eachin]] loops.
+
+Note that you should NOT modify a list while iterating through it with an eachin loop. Doing so while cause a 'concurrent list
+modification' runtime error in debug mode. Please see [[IContainer]] for more information.
 
 #end
 Class List<T> Implements IContainer<T>