For loop picks conversion to an Iterator over .iterator()

42 views
Skip to first unread message

Ondřej Műller

unread,
Jul 18, 2016, 7:26:14 PM7/18/16
to Haxe
It seemed strange because I did not know for loop uses the same mechanism as all of haxe(namely conversions). It makes sense if you try it against Iterator<T> and then Iterable<T> in order.
Instead of checking it in series, it could check in parallel - see if it's directly Iterator<T>, then Iterable<T>, then look for conversions to first, to second... whatever the priority list is.
This would make more sense. Or is this intended?

Dan Korostelev

unread,
Jul 20, 2016, 9:03:55 PM7/20/16
to Haxe
Could you elaborate? What "conversions" are you talking about? Could you show some code to make it easier understand the issue?

вторник, 19 июля 2016 г., 2:26:14 UTC+3 пользователь Ondřej Műller написал:

Ondřej Műller

unread,
Jul 20, 2016, 10:18:58 PM7/20/16
to Haxe
User-defined conversions for abstracts, @:from and @:to.
Example here:

package;

class AClass {
   
public function new() {}
   
   
public function iterator():Iterator<String> return new AIterator(this);
}
@:forward()
abstract A(AClass) from AClass to AClass {
   
public function new() {
       
this = new AClass();
   
}
   
   
@:to
   
function toB() return new B();
}
class AIterator {
   
public function new(a:A) {}
   
   
public function hasNext() return true;
   
public function next() return "AIterator";
}
class B {
   
public function new() {}
   
   
public function hasNext() return true;
   
public function next() return "B";
}
class Main {
   
public static function main() {
       
var iter = new A();
       
for (s in iter) {
            trace
(s);
           
break;
       
}
       
for (s in iter.iterator()) {
            trace
(s);
           
break;
       
}
   
}
}
Reply all
Reply to author
Forward
0 new messages