如何在 Swift 通用 where 子句中添加 OR 条件?

How to add OR condition in Swift generic where clause?

我正在扩展 SCNVector3 来自 Apple SceneKit 的算术运算。我对数字类型使用泛型,但我发现我必须为每个函数至少编写两个版本,因为这不起作用:

extension SCNVector3 {
    static func *<T> (left: SCNVector3, right: T) -> SCNVector3 {
        return SCNVector3(left.x * Float(right), left.y * Float(right), left.z * Float(right))
}

并给出错误 无法使用类型为“(T)”的参数列表调用类型 'Float' 的初始值设定项

查看 Apple 文档中 Float 的 definition,我发现我能得到的最通用的是 init<Source: BinaryFloatingPoint>init<Source: BinaryInteger>

所以我不得不重写扩展如下,为每个操作制作两个几乎相同的函数:

extension SCNVector3 {
    static func *<T> (left: SCNVector3, right: T) -> SCNVector3 where T: BinaryFloatingPoint {
        return SCNVector3(left.x * Float(right), left.y * Float(right), left.z * Float(right))

    static func *<T> (left: SCNVector3, right: T) -> SCNVector3 where T: BinaryInteger {
        return SCNVector3(left.x * Float(right), left.y * Float(right), left.z * Float(right))
}

我可以将 AND 放在 where 子句中 — 通过在 where 之后用逗号分隔两个条件。

但是有什么办法可以把 OR 放在那里吗?

您的基本实施应该从这里开始:

extension SCNVector3 {
    static func * (vector: SCNVector3, scale: Float) -> SCNVector3 {
        return SCNVector3(vector.x * scale, vector.y * scale, vector.z * scale)
    }
}

该向量有 Float 个分量,因此您应该始终只乘以 Float。这就是 Swift 中所有运算符的工作方式。如果你有不同的类型,在乘法之前转换它,而不是乘法的副作用。

如果你真的想传递其他类型,那么你可以使用方法重载:

extension SCNVector3 {
    static func *<T> (left: SCNVector3, right: T) -> SCNVector3 where T: BinaryFloatingPoint {
        return left * Float(right)
    }

    static func *<T> (left: SCNVector3, right: T) -> SCNVector3 where T: BinaryInteger {
        return left * Float(right)
    }
}

无法在类型中定义