在Swift中,〜>(大于等于)运算符是什么?

杰森

Swift 1.1包含〜>运算符的声明:

infix operator ~> {
    associativity left
    precedence 255
}

这在Swift中有什么用?它似乎已声明,但未定义任何可利用它的函数。其他开发人员已将其用于响应模式和封送队列之间的封送,但我想知道为什么在标准框架中对其进行了定义。我猜想它可以“保留”一个自定义运算符供开发人员使用,因为它具有最高的优先级。

肯尼

由于Swift是开源的,我们可以看到~>在stdlib中包含instdlib的实际原因作为Swift 1.x子协议中方法专门化的变通方法

// Workaround for <rdar://problem/14011860> SubTLF: Default
// implementations in protocols.  Library authors should ensure
// that this operator never needs to be seen by end-users.  See
// test/Prototypes/GenericDispatch.swift for a fully documented
// example of how this operator is used, and how its use can be hidden
// from users.
infix operator ~> { associativity left precedence 255 }

可以在test / Prototypes / GenericDispatch.swift中找到详细的示例

注意:请勿~>在Swift 2+中使用这是一个历史性的解决方法。不再需要它。继续阅读。


如何~>工作

在Swift 2中,剩下的唯一实例~>abs函数(它也可能消失了)。我们可以看到实际的~>工作方式。STDLIB /公共/核心/ IntegerArithmetic.swift.gyb,该SignedInteger协议定义了一个~>运算符:

struct _Abs {}

protocol SignedNumber: Comparable {
    prefix func -(_: Self) -> Self

    func ~> (_: Self, _: (_Abs, ()) -> Self
}

func ~> <T: SignedNumber>(x: T, _: (_Abs, ()) -> T {
    return x < 0 ? -x : x
}

在此,箭头的RHS~>指定可以将什么命令发送到对象。想像p~>(cmd, args)p.cmd(args)

然后,我们提供的默认实现_Abs时候给出一个SignedNumber

protocol AbsoluteValuable : SignedNumber {
    static func abs(_: Self) -> Self
}

func ~> <T: AbsoluteValuable>(x: T, _: (_Abs, ())) -> T {
    return T.abs(x)
}

接下来,我们有一个子协议,AbsoluteValuable协议可能比拥有更有效的方法来计算绝对值x < 0 ? -x : x然后,我们专门~>针对AbsoluteValuable

func abs<T: SignedNumber>(_ x: T) -> T {
    return x ~> (_Abs(), ())
}

最后,我们将~>调用隐藏在公共包装器方法中。如果T实际上是AbsoluteValuable,则将选择更专业的方式,因此效率更高~>

这也是为什么我们得到@rintaro42 ~> _advance(12)42 ~> _distanceTo(23)@rintaro的答案所示原因,因为.advanceByand.distanceTo方法对于一般而言是O(n)ForwardIndexType,但是如果类型为,则可以在O(1)中实现RandomAccessIndexType


你不需要 ~>

使用协议扩展,也可以在调用~>操作员的情况下完成此模式

protocol SignedInteger: Comparable {
    prefix func -(_: Self) -> Self

    func genericAbs() -> Self
}

extension SignedInteger {
    func genericAbs() -> Self {
        return self < 0 ? -self : self
    }
}

protocol AbsoluteValueable: SignedInteger {
    static func abs(_: Self) -> Self
}

extension AbsoluteValueable {
    func genericAbs() -> Self {
        return Self.abs(self)
    }
    // normally you would allow subtypes to override
    // genericAbs() directly, instead of overriding 
    // static abs().
}

func abs<T: SignedInteger>(x: T) -> T {
    return x.genericAbs()
}

特别是这就是为什么Swift 2中所有其他~>实现abs都消失的原因:使用此技术的所有专业都已更改为使用协议扩展,例如协议扩展。


注意:雷达问题14011860不是公开的,但是我们可以通过在OpenRadar上重复查找此错误的范围:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章