Swift 泛型运算符

har*_*n78 0 generics operator-overloading swift

我写了一个这样的类:

struct Size {
    var width:Double=0
    var height:Double=0
    init(width:Double=0,height:Double=0)
    {
        self.width=width
        self.height=height
    }
    [...]
}
Run Code Online (Sandbox Code Playgroud)

现在我希望能够为某个数字划分大小,并且我想使用泛型来获取可转换为 Double 的每种类型的函数。例如IntCGFloatFloat

但是当我插入函数时:

func /<T>(lhs:Size,rhs:T)->Size
{
    return Size(width:lhs.width/Double(rhs),height:lhs.height/Double(rhs))
}
Run Code Online (Sandbox Code Playgroud)

我收到错误

error: cannot invoke 'init' with an argument list of type '(width: Double, height: Double)'
        return Size(width:Double(lhs.width/rhs),height:Double(lhs.height/rhs))
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

这真的很奇怪,因为传递的列表是类(Double)中定义的确切类型。

如果我以这种方式重写它:

func /<T>(lhs:Size,rhs:T)->Size
{
    let drhs=Double(rhs)
    return Size(width:lhs.width/drhs,height:lhs.height/drhs)
}
Run Code Online (Sandbox Code Playgroud)

然后我得到错误:

error: cannot invoke 'init' with an argument of type 'T'
    let drhs=Double(rhs)
             ^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

这更奇怪,因为 Swift 库文件有很多 Double、Int 或 Float 类型的初始化器:

extension Double {
    init(_ v: UInt8)
    init(_ v: Int8)
    init(_ v: UInt16)
    init(_ v: Int16)
    init(_ v: UInt32)
    init(_ v: Int32)
    init(_ v: UInt64)
    init(_ v: Int64)
    init(_ v: UInt)
    init(_ v: Int)
}

extension Double {
    init(_ v: Float)
    init(_ v: Float80)
}
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题?

Man*_*ala 5

在 swift 中,泛型需要类型安全,这是通过协议对其进行约束来实现的。这比在 C++ 中要好得多,在 C++ 中不能用泛型来假设安全性。通常,您可以使用标准库中的协议来实现这些泛型,但不幸的是,在您的情况下,您需要定义一个特殊的协议并扩展您希望能够使用的所有类型。我已经为 Int 和 Float 完成了它,但是您可以对要使用的所有类型执行相同的操作。

protocol DoubleConvertible
{
    func toDouble() -> Double
}
extension Int : DoubleConvertible
{
    func toDouble() -> Double
    {
        return Double(self)
    }
}
extension Float: DoubleConvertible
{
    func toDouble() -> Double
    {
        return Double(self)
    }
}
func /<T : DoubleConvertible>(lhs: Size, rhs: T) -> Size
{
    return Size(width: lhs.width / rhs.toDouble(), height: lhs.height / rhs.toDouble())
}
Run Code Online (Sandbox Code Playgroud)

您必须手动执行此操作的原因是因为标准库协议(我所知道的)都没有定义可以将数字转换为 Double 的要求。