On reading Apple's Swift Programming Language guide regarding opaque types, there's one paragraph I don't understand. The guide is discussing the differences between opaque types and protocols, and states that you can't nest calls that return a protocol type. They use this code fragment, where Shape is a protocol:
func protoFlip<T: Shape>(_ shape: T) -> Shape {
if shape is Square {
return shape
}
return FlippedShape(shape: shape)
}
It then states that:
Another problem with this approach is that the shape transformations don’t nest. The result of flipping a triangle is a value of type Shape, and the protoFlip(:) function takes an argument of some type that conforms to the Shape protocol. However, a value of a protocol type doesn’t conform to that protocol; the value returned by protoFlip(:) doesn’t conform to Shape. This means code like protoFlip(protoFlip(smallTriange)) that applies multiple transformations is invalid because the flipped shape isn’t a valid argument to protoFlip(_:).
However, I wrote this code:
import Foundation
protocol P {
associatedtype AT
}
struct C: P {
typealias AT = Int
}
func f<T: P>(_ t: T) -> T {
t
}
func g() {
f(f(C()))
}
g()
and this compiles and runs...and appears to let me nest those calls.
What am I mis-understanding? What is the documentation trying to say?