enum Maybe { Nothing; Just(v : T); } class QueryIterator { var result : Dynamic; var nextValue : Maybe; public function new(result) { this.result= result; /* We fetch the first row , so we can answer hasNext */ fetch(); } private function fetch() { var next : Dynamic = untyped __call__('db_fetch', this.result); nextValue = if(next) Just(next) else Nothing; } public function hasNext() : Bool { return this.nextValue != Nothing; } public function next() : T { switch(this.nextValue) { case Nothing : throw 'Iterator exhausted'; case Just(v) : { this.fetch(); return v; }; } } }