1

我有一些protocols用于vectors我的应用程序。他们有一些default implementationsextensions所以我可以为所有人实现所有添加的功能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 的所有其他类型单独实现它。我也将它用于SCNVector3CGPoint。我不知道什么更好。也许有一些更好的解决方案,这样我就可以为除 SIMD3 之外的所有其他类型实现这个?

4

1 回答 1

1

好的,我想我找到了一个更好的解决方案。BasicMathOperations我将from的默认实现移动extension Vector到单独的协议,然后我将这个新协议的继承添加到所有符合 Vector 的类型,SIMD3 除外。

// Adds default implementation for BasicMathOperation
protocol VectorWithDefaultImplementationForBasicMathOperations: Vector {}

extension VectorWithDefaultImplementationForBasicMathOperations {
    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, /) }
}

extension CGPoint: VectorWithDefaultImplementationForBasicMathOperations {}
extension SCNVector3: VectorWithDefaultImplementationForBasicMathOperations {}

如果有人知道更好的解决方案,请告诉我,但我认为这已经很好了。

于 2019-04-03T09:39:24.687 回答