haxe icon indicating copy to clipboard operation
haxe copied to clipboard

IntIterators are not Iterable

Open kaug-whis opened this issue 1 year ago • 4 comments

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.

kaug-whis avatar May 10 '24 03:05 kaug-whis

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.

Simn avatar May 10 '24 06:05 Simn

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

Apprentice-Alchemist avatar May 11 '24 16:05 Apprentice-Alchemist

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

Simn avatar May 11 '24 17:05 Simn

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);
}

kLabz avatar May 11 '24 18:05 kLabz