# For-in vs forEach() when sequence element is Dictionary.Key

**URL:** <https://forums.swift.org/t/for-in-vs-foreach-when-sequence-element-is-dictionary-key/64053>\
**Category:** Using Swift\
**Tags:** generics\
**Created:** [March 29, 2023, 1:04pm UTC](https://forums.swift.org/t/for-in-vs-foreach-when-sequence-element-is-dictionary-key/64053 "2023-03-29T13:04:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![olex.l](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/olex.l/32/29355_2.png) [@olex.l](https://forums.swift.org/u/olex.l)\
**Post date:** [March 29, 2023, 1:04pm UTC](https://forums.swift.org/t/for-in-vs-foreach-when-sequence-element-is-dictionary-key/64053/1 "2023-03-29T13:04:46Z")

</div>

Consider the following extension:

```swift
extension Dictionary {
    func f1(keys: any Sequence<Key>) {
        for key in keys {
            let value = self[key]
        }
    }
    func f2(keys: any Sequence<Key>) {
        keys.forEach { key in
            let value = self[key]
        }
    }
}

```

In `for-in` the type of `key` is inferred as `Any`, while in `forEach()` its type is `Key`. Is this by design or a bug?

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [March 29, 2023, 2:40pm UTC](https://forums.swift.org/t/for-in-vs-foreach-when-sequence-element-is-dictionary-key/64053/2 "2023-03-29T14:40:21Z")

</div>

First off, you probably know this, but both `f1` and `f2` should really take arguments of type `some Sequence<Key>`, not `any`, which would make the error go away.

* * *

As I understand it, this is a known limitation that could be lifted in the future. Let me try to break it down.

Starting with `f2`: `forEach` can preserve the concrete `Key` type thanks to [Covariant Erasure with Constrained Existentials](https://github.com/apple/swift-evolution/blob/main/proposals/0353-constrained-existential-types.md#covariant-erasure-with-constrained-existentials) in SE-0353: Constrained Existential Types. So this is relatively straightforward.

* * *

In the case of `f1`, the compiler essentially rewrites the `for` loop into a `while` loop, like this:

```swift
var iterator = keys.makeIterator()
while let key = iterator.next() {
    …
}

```

If we write this `while` loop by hand, we get an error on the `makeIterator()` call in the first line:

```swift
error: inferred result type 'any IteratorProtocol' requires explicit
coercion due to loss of generic requirements

```

The compiler forces us to write `keys.makeIterator() as any IteratorProtocol` to acknowledge that the `iterator` variable loses the knowledge that its `Element == Key`, and this is the reason why the element type in `Any` in your `for` loop.

Pretty much this exact situation is described in ["Losing" constraints when type-erasing resulting values](https://github.com/apple/swift-evolution/blob/main/proposals/0352-implicit-open-existentials.md#losing-constraints-when-type-erasing-resulting-values) in SE-0352: Implicitly Opened Existentials:

> When the result of a call involving an opened existential is type-erased, it is possible that some information about the returned type cannot be expressed in an existential type, so the "upper bound" described above will lose information.

That section also mentions that future compiler versions could increase the expressivity of the language by recovering more type information that right now. It even makes explicit mention of primary associated types SE-0353 that could help with this.

In summary, it seems like the compiler could (and should, eventually) preserve the element type information when making an iterator from an `any Sequence<T>`, but that hasn't been implemented yet (and if implemented, it presumably would also have to go through Swift Evolution).

---

<div class="post-metadata">

**Author:** ![olex.l](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/olex.l/32/29355_2.png) [@olex.l](https://forums.swift.org/u/olex.l)\
**Post date:** [April 1, 2023, 4:26pm UTC](https://forums.swift.org/t/for-in-vs-foreach-when-sequence-element-is-dictionary-key/64053/3 "2023-04-01T16:26:49Z")

</div>

Thank you for the explanation. Complex stuff, though still interesting.
