1class Foo { 2} 3 4class FooIterator { 5 bool called = false; 6 7 public bool next () { 8 return !called; 9 } 10 11 public Foo @get () { 12 assert (!called); 13 called = true; 14 return foo_instance; 15 } 16} 17 18class FooCollection { 19 public FooIterator iterator () { 20 return new FooIterator (); 21 } 22} 23 24class FooIterator2 { 25 bool called = false; 26 27 public Foo? next_value () { 28 if (called) 29 return null; 30 called = true; 31 return foo_instance; 32 } 33} 34 35class FooCollection2 { 36 public FooIterator2 iterator () { 37 return new FooIterator2 (); 38 } 39} 40 41class FooCollection3 { 42 public int size { get { return 1; } } 43 44 public Foo @get (int index) { 45 assert (index == 0); 46 return foo_instance; 47 } 48} 49 50class FooEntry4<K,V> { 51 public K key { get; private set; } 52 public V value { get; private set; } 53 54 public FooEntry4 (K _key, V _value) { 55 key = _key; 56 value = _value; 57 } 58} 59 60class FooIterator4<G> { 61 bool called = false; 62 63 public bool next () { 64 return !called; 65 } 66 67 public G @get () { 68 assert (!called); 69 called = true; 70 return new FooEntry4<string,Foo> ("foo", foo_instance); 71 } 72} 73 74class FooCollection4<K,V> { 75 public int size { get { return 1; } } 76 77 public V @get (K key) { 78 assert (key == "foo"); 79 return foo_instance; 80 } 81 82 public FooIterator4<FooEntry4<K,V>> iterator () { 83 return new FooIterator4<FooEntry4<K,V>> (); 84 } 85} 86 87Foo foo_instance; 88 89void main () { 90 foo_instance = new Foo (); 91 92 // Uses next() and get() 93 var collection = new FooCollection (); 94 foreach (var foo in collection) { 95 assert (foo == foo_instance); 96 } 97 98 // Uses next_value() 99 var collection2 = new FooCollection2 (); 100 foreach (var foo2 in collection2) { 101 assert (foo2 == foo_instance); 102 } 103 104 // Uses size and get() 105 var collection3 = new FooCollection3 (); 106 foreach (var foo3 in collection3) { 107 assert (foo3 == foo_instance); 108 } 109 110 // Uses iterator() and get() 111 var collection4 = new FooCollection4<string,Foo> (); 112 foreach (var fooentry4 in collection4) { 113 assert (fooentry4.key == "foo"); 114 assert (fooentry4.value == foo_instance); 115 } 116 assert (collection4["foo"] == foo_instance); 117 118 // GLib.List 119 var list = new List<Foo> (); 120 list.append (foo_instance); 121 foreach (var e in list) { 122 assert (e == foo_instance); 123 } 124 125 // GLib.SList 126 var slist = new SList<Foo> (); 127 slist.append (foo_instance); 128 foreach (var e in slist) { 129 assert (e == foo_instance); 130 } 131} 132