HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.03k stars 648 forks source link

IntIterators are not Iterable #11664

Open kaug-whis opened 1 month ago

kaug-whis commented 1 month ago
using Lambda;
...
(1...3).iter(i->trace(i));

The above seems like it should be valid syntax, but since IntIterator does not have an iterator():Iterator<Int> method to allow it to work as an Iterable, it does not work.

Simn commented 1 month ago

The real problem here is that Lambda works with Iterable, not Iterator. That's where this issue should be addressed, but I don't know how exactly to do that.

Apprentice-Alchemist commented 1 month ago

Making Lambda work with both would be relatively simple, for example using an abstract with implicit conversions from both Iterable and Iterator: https://try.haxe.org/#8e6b3e3d

Simn commented 1 month ago

Huh, I thought we disallowed the combination of static extensions and implicit casts, but I guess I'm thinking of something else.

kLabz commented 1 month ago

Right, it doesn't work with [0,1,2].iter(item -> trace(item));

And this looks silly :sweat_smile:

overload extern inline function iter<T>(it:Iterable<T>, fn:T->Void) {
    for (item in it)
        fn(item);
}

overload extern inline function iter<T>(it:Iterator<T>, fn:T->Void) {
    for (item in it)
        fn(item);
}