# Why 'any' needed when associated type known

**URL:** <https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502>\
**Category:** Discussion\
**Tags:** generics, protocols, associatedtype\
**Created:** [November 17, 2022, 12:12pm UTC](https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502 "2022-11-17T12:12:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrtsamma](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrtsamma/32/23055_2.png) [@mrtsamma](https://forums.swift.org/u/mrtsamma)\
**Post date:** [November 17, 2022, 12:12pm UTC](https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502/1 "2022-11-17T12:12:43Z")

</div>

I'm happy that Swift has introduced the primary associated type for protocols.  
But I feel like they need some boost/improvement.  
Cause currently, it's super confusing why we can't use protocols with a known primary associated type the same way as we use non-generic protocols.

We can currently use for regular protocols:

```swift
protocol RegularProtocol {}
var listOfRegularProtocol = [RegularProtocol]()

```

But when we have:

```swift
protocol SomeGenericProtocol<T> {
	associatedtype T
	var value: T { get }
}

typealias IntValueProtocol = SomeGenericProtocol<Int>
// ❎ Use of 'IntValueProtocol' (aka 'SomeGenericProtocol<Int>') as a type must be written 'any IntValueProtocol' (aka 'any SomeGenericProtocol<Int>')
let intValueProtocols = [IntValueProtocol]()
// ✅ needs any keyword
let intValueProtocolsSuccess = [any IntValueProtocol]()

```

But we know that type T can't be anything else than Int.  
It will be equivalent to:

```swift
protocol SomeProtocol {
	var value: Int { get }
}
let someProtocols = [SomeProtocol]()

```

This works but if the same declaration would be `SomeProtocol: SomeGenericProtocol<Int>` then again we need any for usage.

And the same for:

```swift
protocol IKnowThatAssociatedTypeIsInt: SomeGenericProtocol where T == Int {}
let intValueProtocols2 = [any IKnowThatAssociatedTypeIsInt]()

```

Is there any change that we get rid of needed any keyword where we have declared the associated type?

---

<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:** [November 17, 2022, 12:27pm UTC](https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502/2 "2022-11-17T12:27:40Z")

</div>

> [@mrtsamma](#):
>
> ```swift
> let someProtocols = [SomeProtocol]()
> 
> ```
> 
> This works

Hi @mrtsamma, this works right now for source compatibility reasons, but it will not work in Swift 6. In other words, Swift will always require the `any` keyword soon.

---

<div class="post-metadata">

**Author:** ![mrtsamma](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mrtsamma/32/23055_2.png) [@mrtsamma](https://forums.swift.org/u/mrtsamma)\
**Post date:** [November 17, 2022, 12:30pm UTC](https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502/3 "2022-11-17T12:30:13Z")

</div>

Ok. That explains it. Thanks

---

<div class="post-metadata">

**Author:** ![Gero](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gero/32/23571_2.png) [@Gero](https://forums.swift.org/u/Gero)\
**Post date:** [November 17, 2022, 12:30pm UTC](https://forums.swift.org/t/why-any-needed-when-associated-type-known/61502/4 "2022-11-17T12:30:15Z")

</div>

I'm not sure, but I think it was at least discussed to actually do the opposite and at some point in the future _always require_ `any` if you are using a protocol as a type.

Conceptually you still do not know what concrete type will be stored in e.g. the `intValueProtocols` variable in your example. Just that it adopts `IntValueProtocol`.

Since before we had `any` at all protocols _without_ associated types could simply be used as a type to denote storage we now have a kind of "legacy hole". I don't know which direction this will go, but there's a point to be made to say "we better always explicitly denote when we want a variable that can hold _any type that adopts a protocol_", regardless of whether that protocol has associated types or not.

edit:  
I guess @xwu clarified this then. :) Thanks! Personally I am all for requiring `any` in the future.
