I've encountered an interesting issue related to protocol associated types and their conformance to AdditiveArithmetic, specifically when working with SIMD3<Float>. Despite SIMD3<Float> conforming to AdditiveArithmetic, the Swift compiler fails to recognize this conformance in the context of a protocol's associated type constraint.
Consider the following code snippet:
protocol P {
associatedtype A: AdditiveArithmetic
}
struct MyFloat: P {
typealias A = Float
// Okay
}
struct MyVec3: P {
typealias A = SIMD3<Float>
// Compiler Error: Type `MyVec3` does not conform to protocol 'P'
// Compiler Note: Possibly intended match 'MyVec3.A' (aka 'SIMD3<Float>') does not conform to 'AdditiveArithmetic'
}
// Adding this extension resolves the compiler error
extension SIMD3<Float>: AdditiveArithmetic {}
tera
2
It isn't:
func foo(_ x: any AdditiveArithmetic) {}
foo(SIMD3<Float>()) // 🛑 Argument type 'SIMD3<Float>' does not conform to expected type 'AdditiveArithmetic'