Haxe: Null object pattern for iterables -
i'd implement null object design pattern iterable class. example if internal array isn't initialized, wrapper class anyway returns empty iterator doesn't break main logic:
public function iterator():iterator<t> { // ...of cause doesn't work, because iterator typedef not class return mlist != null ? mlist.iterator() : new iterator<t>(); } var mlist:array<t>;
should instantiate static empty dummy array desired type of items or else implements iterator interface contains nothing? or may there more straight solution?
you check @ forehand in object class itself, adding kind of isempty function:
public function isempty():bool { return mlist == null || mlist.length == 0; }
then use this:
if(!iter.isempty()) { for(i in iter) { trace(i); } }
example: http://try.haxe.org/#8719e
or
you use dummy iterator this:
class nulliterator { public inline function hasnext() return false; public inline function next() return null; public inline function new() {} }
.. , use
public function iterator():iterator<t> { return mlist != null ? mlist.iterator() : new nulliterator(); }
example: http://try.haxe.org/#b2d7e
if think behavior should changed, raise issue on github. https://github.com/haxefoundation/haxe/issues
Comments
Post a Comment