# Understating primary associated types in Swift 5.7

**URL:** <https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916>\
**Category:** Using Swift\
**Created:** [August 26, 2022, 5:57pm UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916 "2022-08-26T17:57:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmihai](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danmihai/32/21688_2.png) [@danmihai](https://forums.swift.org/u/danmihai)\
**Post date:** [August 26, 2022, 5:57pm UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/1 "2022-08-26T17:57:22Z")

</div>

To begin with the beginning, I'm not quite sure what _primary associated types_ are in the first place.  
I know they are marked by _generics_ such as:

```swift
struct Collection<Element> {
     associatedtype Element
     ....
}

```

And that now you can do something like `some Collection<any Type>`, although that seems to introduce some unwelcomed complexity to me, because now I feel I could also write `some Collection<any Type>`? But probably that doesn't make any sense whatsoever. Not sure.

But my main question is what's the difference between an associated type and a primary associated type? And how does one distinguish between them?  
Thanks!

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [August 26, 2022, 8:32pm UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/2 "2022-08-26T20:32:18Z")

</div>

> [@danmihai](#):
>
> But my main question is what's the difference between an associated type and a primary associated type? And how does one distinguish between them?  
> Thanks!

every primary associated type (PAT) is an associated type, but not every associated type is a PAT.

```swift
where T:Collection<U>

```

is just syntactic sugar for

```swift
where T:Collection, T.Element == U

```

---

<div class="post-metadata">

**Author:** ![danmihai](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danmihai/32/21688_2.png) [@danmihai](https://forums.swift.org/u/danmihai)\
**Post date:** [August 27, 2022, 3:57am UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/3 "2022-08-27T03:57:23Z")

</div>

I see, thank you! But if Collection already has Element as a generic parameter in angle brackets, why bother putting it as an associated type?

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [August 27, 2022, 4:41am UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/4 "2022-08-27T04:41:18Z")

</div>

Collection is a protocol, it doesn’t (and can’t) have generic parameters. Protocols just have associated types, whether primary or not.

---

<div class="post-metadata">

**Author:** ![danmihai](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danmihai/32/21688_2.png) [@danmihai](https://forums.swift.org/u/danmihai)\
**Post date:** [August 27, 2022, 5:36am UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/5 "2022-08-27T05:36:15Z")

</div>

I just found out about that! So the the only reason for "generics", aka bracket parameters in Protocols, at all, is that so you can write something like `func returnACollection -> Collection <any SomeProtocol>`? Or something similar.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [August 27, 2022, 7:05am UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/6 "2022-08-27T07:05:27Z")

</div>

Yup: [swift-evolution/0346-light-weight-same-type-syntax.md at main · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md)

---

<div class="post-metadata">

**Author:** ![japanese-goblinn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/japanese-goblinn/32/32877_2.png) [@japanese-goblinn](https://forums.swift.org/u/japanese-goblinn)\
**Post date:** [August 27, 2022, 9:33pm UTC](https://forums.swift.org/t/understating-primary-associated-types-in-swift-5-7/59916/7 "2022-08-27T21:33:28Z")

</div>

I think the proposal tells everything about why this is awesome. Also because of this feature and some improvement to generics now it's more natural to work with combine and you don't need to put in this existential wrapper

```swift
// instead of this
var intPublisher: AnyPublisher<Int, Never> { 
  Just(1).eraseToAnyPublisher()
}

// you can just
var intPublisher: some Publisher<Int, Never> { 
  Just(1)
}

```
