具有默认实现的SIMD3扩展

问题描述 投票:0回答:1

我在我的应用程序中使用了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的所有其他类型单独实现它。我也用它来为SCNVector3CGPoint。我不知道什么是更好的。也许有一些更好的解决方案,所以我可以为除SIMD3之外的所有其他类型实现这个?

swift protocols generic-programming swift-protocols swift5
1个回答
0
投票

好的,我想我找到了一个更好的解决方案。我将BasicMathOperations的默认实现从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 {}

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

© www.soinside.com 2019 - 2024. All rights reserved.