Time for action – Making it possible to iterate over a fridge

Now, everything is working as expected but we want to add the possibility to iterate over our Fridge.

  1. For that to happen, as we have seen before, we need to have an iterator function that will take no argument and return an iterator. So let's write it:
       public function iterator()
       {
          return new StorableIterator(this);
       }
  2. Since this is declared in the iterable typedef, we don't have to mark our class as implementing iterable. So our class now looks like this:
    package fridgeManager; class Fridge { public var storedItems : List<Storable>; public function new() { storedItems = new List<Storable>(); } public function addItem(item : Storable) { storedItems.push(item); item.storedOn = Date.now(); ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.