O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(); } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required