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:
protocol RegularProtocol {}
var listOfRegularProtocol = [RegularProtocol]()
But when we have:
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:
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:
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?