HaxeFoundation / HaxeFoundation/haxe

Iterable instead of Iterator as return type in std

Open
#9,656 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

An alternative take on #1914. I think the discussion started from wrong angle - instead of talking about how to make `Iterator` (mutable one-time use entity) usable in place where `Iterable` (immutable multi-use entity) is expected, question should be asked why do we get `Iterator` and not `Iterable` in the first place.

In standard library when a method returns something for iteration (like `map.keys()` in the mentioned issue) it is always `Iterator` and not `Iterable`. Even though in most cases under the hood an iterable is retrieved or created and then an iterator of that iterable returned. So why not return iterable itself and not its less useful derivative?

Two main advantages for `Iterable` over `Iterator` is that the former can be reused in multiple iterations and that std has utils to work with it - `Lambda`. And if `Iterable` implementation class is inlinable then for a common use case of a single for-loop iteration produced code is identical (as it should be) after inlining and optimisation.

It is a breaking change. But migration path for a user is pretty straight-forward. For for-loops nothing will change as they work with both types and for cases where you for some reason need an iterator instance - just add `iterator()` call.

Am i missing some drawbacks regarding returning `Iterable` itself?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.