# Expose IndexingIterator's initializer

**URL:** <https://forums.swift.org/t/expose-indexingiterators-initializer/47571>\
**Category:** Pitches\
**Created:** [April 20, 2021, 6:44pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571 "2021-04-20T18:44:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [April 20, 2021, 6:44pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571/1 "2021-04-20T18:44:14Z")

</div>

For some reason, `IndexingIterator`'s initializer, despite being `public`, contains underscores which prevent it from appearing in code-completion, and seem to suggest that it shouldn't be used.

> <https://github.com/apple/swift/blob/4255a670c43a1350c663786953e8f509d5718a96/stdlib/public/core/Collection.swift#L88>

I'd like to pitch removing those underscores. I can't think of a reason why we'd want to prevent users from creating an `IndexingIterator` starting at a particular `Index`.

It's essentially possible to do this already by creating an iterator over a slice, but doing so changes the iterator's type, making it an `IndexingIterator<Slice<MyCollection>>` rather than an `IndexingIterator<MyCollection>`.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 20, 2021, 8:20pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571/2 "2021-04-20T20:20:32Z")

</div>

These entry points are public only for standard library testing purposes.

As I understand it, there’s no intention to make it possible for the end user to initialize an `IndexingIterator` _at all_. Instead, users obtain an instance by calling `makeIterator()` on a collection type that doesn’t override the default implementation. But there shouldn’t ever be a need to support mixing-and-matching the default iterator with a collection type that provides a custom iterator.

> [@Karl](#):
>
> It's essentially possible to do this already by creating an iterator over a slice, but doing so changes the iterator's type, making it an `IndexingIterator<Slice<MyCollection>>` rather than an `IndexingIterator<MyCollection>` .

Why’s the concrete iterator type a problem?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [April 20, 2021, 8:35pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571/3 "2021-04-20T20:35:29Z")

</div>

> [@xwu](#):
>
> But there shouldn’t ever be a need to support mixing-and-matching the default iterator with a collection type that provides a custom iterator.

Hm? Not sure what you mean here.

> [@xwu](#):
>
> Why’s the concrete iterator type a problem?

Why _wouldn't_ it be a problem? `MyCollection`'s iterator type is `IndexingIterator<MyCollection>`, not `IndexingIterator<Slice<MyCollection>>`, so it isn't able to return an offset iterator as an instance of its own iterator.

If this function isn't supported, it means that we require users to write their own iterators - even when the default would have been fine - just to support this obvious functionality. It isn't very difficult, but it is _very_ annoying to have to reimplement basic types like this.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 20, 2021, 10:43pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571/4 "2021-04-20T22:43:16Z")

</div>

> [@Karl](#):
>
> Why _wouldn't_ it be a problem? `MyCollection` 's iterator type is `IndexingIterator<MyCollection>` , not `IndexingIterator<Slice<MyCollection>>` , so it isn't able to return an offset iterator as an instance of its own iterator.
> 
> If this function isn't supported, it means that we require users to write their own iterators - even when the default would have been fine - just to support this obvious functionality. It isn't very difficult, but it is _very_ annoying to have to reimplement basic types like this.

I’m not sure I understand. There is no publicly documented API to instantiate `IndexingIterator` _at all_, unless I’m missing something. Can you describe the use case you have in mind?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 20, 2021, 10:53pm UTC](https://forums.swift.org/t/expose-indexingiterators-initializer/47571/5 "2021-04-20T22:53:57Z")

</div>

> [@Karl](#):
>
> > [@xwu](#):
> >
> > But there shouldn’t ever be a need to support mixing-and-matching the default iterator with a collection type that provides a custom iterator.
> 
> Hm? Not sure what you mean here.

Where a type defines its own custom iterator, it is not the intention of Swift to support the end user instantiating `IndexingIterator(elements: valueOfMyCollectionTypeWithCustomIterator)`. They should always use `valueOfMyCollectionTypeWithCustomIterator.makeIterator()`.
