Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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();
       }
       
       public function removeItem(item : Storable)
       {
          storedItems.remove(item);
          item.storedOn = null;
       }
       
       public function iterator()
       {
          return...