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

Den*_*uev 6 generics where swift

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

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

并给出错误无法使用类型'(T)'的参数列表调用Type'Float'的初始化程序

看一下Apple文档中Float的定义,我发现我能得到的最通用的是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))
}
Run Code Online (Sandbox Code Playgroud)

我可以将AND放在where子句中 - 用where逗号分隔后有两个条件.

但有什么方法可以放在那里吗?

Sul*_*han 6

您的基本实现应该以此开始:

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

该向量具有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)
    }
}
Run Code Online (Sandbox Code Playgroud)

没有办法如何定义or类型。