Xcode 14 RC: "Cannot specialize protocol type"

That's right. Primary associated types were adopted in the standard library in SE-0358: Primary Associated Types in the Standard Library. Xcode 14 RC has the macOS 12.5 SDK, where this standard library change is not included.

To summarize, with the Swift 5.7 compiler you can:

  • Write your own protocols with primary associated types
  • Use primary associated type constraints for parameters and opaque return types
  • Write any types with primary associated type constraints.

For these generics features, the things where the Swift 5.7 runtime is required (and thus need to be deployment target or availability gated) are:

  • Dynamic casts to any types with primary associated type constraints
  • any types with primary associated type constraints used as generic arguments, e.g. Array<any Collection<Int>>

Both of these limitations can be worked around by writing your own AnyP<T> struct that operates on any P<T> in its implementation.

To write primary associated type constraints on the standard library protocols that have adopted primary associated types, e.g. Collection<Int>, you need to build against an SDK with the Swift 5.7 standard library, which for Apple platforms are the SDKs for iOS 16, iPadOS 16, tvOS 16, watchOS 9, and macOS 13.

15 Likes