6.20.2.1 collections.iter(T)

The collections.iter(T) templated module provides structs Iter_T and Iterable_T that can be convenient for defining iterators. Additionally, there are several utilities for manipulating iterators of these types. Technically, the syntax for(T x : a) does not require anything from this module, but several utilities only work for iterables of type Iterable_T.

The Iter_T structure has the following fields, all of which are null by default:

Iter_T is intended as the return type of an operator iter method; See Iterators for more information on how to use these fields.

Iterable_T is a generic structure for something that has an operator iter method returning an Iter_T. This structure, not Iter_T, is the type that can appear inside a range-based for loop. Consequently, most of the utilities below manipulate instances of Iterable_T rather than Iter_T.

It is good practice for a structure that defines an operator iter method to also define an implicit cast to Iterable_T for better compatibility with the utilities in this module. Additionally, an instance a of Iterable_T can be cast to a T[] with the syntax (T[]) a. (This is an explicit cast, so it requires parentheses.) Going the other way, a T[] can be implicitly cast to an Iterable_T (no parentheses required) as long as the appropriate Iterable_T type has been accessed (see Autounravel). This can be done explicitly using the range function listed below.

The collections.iter module provides the following functions:

Iterable_T Iterable(Iter_T iter())

creates an Iterable_T with its operator iter method set to iter (autounraveled whenever Iterable_T is accessed). This function is an alias for the constructor Iterable_T(Iter_T iter()).

Iterable_T range(T[] items)

creates an Iterable_T that iterates over the elements of items. Separate calls to operator iter return independent iterators.

Iter_T Iter_T(T[] items)

creates an Iter_T that iterates over the elements of items.