我有一些protocols
用于vectors
我的应用程序。他们有一些default implementations
,extensions
所以我可以为所有人实现所有添加的功能types of vectors
。现在我想扩展SIMD3
,Scalar == Double
来实现 Vector3D 协议。Swift
告诉我,如果我Scalar
在扩展中指定类型,那么我还需要Vector3D
为此类型添加所有依赖项。我不知道为什么当我选择关联类型时这不会自动发生SIMD3
,但是好的。所以现在我有这样的事情:
import UIKit
import simd
protocol DividableByInt {
static func / (lhs: Self, rhs: Int) -> Self
}
protocol HasBasicinitializer {
init()
}
protocol BasicMathOperations {
static func + (lhs: Self, rhs: Self) -> Self
static func - (lhs: Self, rhs: Self) -> Self
static func * (lhs: Self, rhs: Self) -> Self
static func / (lhs: Self, rhs: Self) -> Self
}
protocol Vector: BasicMathOperations, DividableByInt, HasBasicinitializer {
associatedtype Scalar: (SIMDScalar & FloatingPoint)
static var zero: Self { get }
static func calculate(_ lhs: Self, _ rhs: Self, _ operation: (Scalar, Scalar) -> Scalar) -> Self
static func calculate(_ lhs: Self, _ rhs: Scalar, _ operation: (Scalar, Scalar) -> Scalar) -> Self
func allAxesValues() -> [Scalar]
}
extension Vector {
static func + (lhs: Self, rhs: Self) -> Self { return calculate(lhs, rhs, +) }
static func - (lhs: Self, rhs: Self) -> Self { return calculate(lhs, rhs, -) }
static func * (lhs: Self, rhs: Self) -> Self { return calculate(lhs, rhs, *) }
static func / (lhs: Self, rhs: Self) -> Self { return calculate(lhs, rhs, /) }
static func * (lhs: Self, rhs: Scalar) -> Self { return calculate(lhs, rhs, *) }
static func / (lhs: Self, rhs: Scalar) -> Self { return calculate(lhs, rhs, /) }
static func / (lhs: Self, rhs: Int) -> Self { return calculate(lhs, Scalar(rhs), /) }
}
protocol Vector3D: Vector {
init(x: Scalar, y: Scalar, z: Scalar)
var x: Scalar { get }
var y: Scalar { get }
var z: Scalar { get }
}
extension Vector3D {
func allAxesValues() -> [Scalar] {
return [x, y, z]
}
static func calculate(_ lhs: Self, _ rhs: Self, _ operation: (Scalar, Scalar) -> Scalar) -> Self {
return Self(x: operation(lhs.x, rhs.x), y: operation(lhs.y, rhs.y), z: operation(lhs.z, rhs.z))
}
static func calculate(_ lhs: Self, _ rhs: Scalar, _ operation: (Scalar, Scalar) -> Scalar) -> Self {
return Self(x: operation(lhs.x, rhs), y: operation(lhs.y, rhs), z: operation(lhs.z, rhs))
}
}
extension SIMD3: Vector3D where Scalar == Double {}
extension SIMD3: HasBasicinitializer {}
extension SIMD3: DividableByInt where Scalar == Double {}
extension SIMD3: Vector where Scalar == Double {
static let zero = SIMD3(x: 0.0, y: 0.0, z: 0.0)
}
extension SIMD3: BasicMathOperations where Scalar == Double {}
一切都会自动运行,除了最后一件事:
extension SIMD3: BasicMathOperations where Scalar == Double {}
编译器说:
Type 'SIMD3<Scalar>' does not conform to protocol 'BasicMathOperations'
但是自从我添加
extension SIMD3: Vector where Scalar == Double
它应该已经实现了所有需要的方法,并且能够继续。协议DividableByInt
的继承方式几乎相同,它可以与Vector extension
. 为什么BasicMathOperations
不能使用实现的方法Vector extension
?
我知道我可以通过添加来解决这个问题
extension SIMD3: BasicMathOperations where Scalar == Double {
static func + (lhs: SIMD3, rhs: SIMD3) -> SIMD3 { return calculate(lhs, rhs, +) }
static func - (lhs: SIMD3, rhs: SIMD3) -> SIMD3 { return calculate(lhs, rhs, -) }
static func * (lhs: SIMD3, rhs: SIMD3) -> SIMD3 { return calculate(lhs, rhs, *) }
static func / (lhs: SIMD3, rhs: SIMD3) -> SIMD3 { return calculate(lhs, rhs, /) }
}
但我不想加倍这段代码,因为它已经在 中实现了extension Vector
,应该从那里使用它。
最后我只想扩展SIMD3
实现Vector3D
。如果需要,Scalar == Double
可以使用任何类型的标量。
看起来问题是因为 SIMD3 已经实现了 +、-、/、* 等函数,编译器无法确定选择哪一个。我可以从中删除默认实现Vector
,这将解决 SIMD3 的问题,但是我需要为符合 Vector 的所有其他类型单独实现它。我也将它用于SCNVector3
和CGPoint
。我不知道什么更好。也许有一些更好的解决方案,这样我就可以为除 SIMD3 之外的所有其他类型实现这个?