Swift中使用的~>(波浪号大于)运算符是什么?

Swift中使用的~>(波浪号大于)运算符是什么?,swift,Swift,Swift 1.1包括~>运营商的声明: infix operator ~> { associativity left precedence 255 } struct _Abs {} protocol SignedNumber: Comparable { prefix func -(_: Self) -> Self func ~> (_: Self, _: (_Abs, ()) -> Self } func ~> <T:

Swift 1.1包括~>运营商的声明:

infix operator ~> {
    associativity left
    precedence 255
}
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
}

这在Swift中用于什么?它似乎是声明的,但没有定义利用它的函数。其他开发人员已经将它用于反应模式和队列之间封送闭包,但我想知道为什么它是在标准框架中定义的。我推测它是为了保留一个自定义操作符供开发人员使用,因为它具有尽可能高的优先级。

它似乎是相关的集合/序列/索引类型

根据:


我不知道这些是如何被使用的,但是:-/

因为Swift是开源的,我们可以看到将~>包含在stdlib中的实际原因:

这里,箭头~>的RHS指定可以发送给对象的命令。把p~>cmd、args想象成类似于p.cmdargs的东西

然后,当给定SignedNumber时,我们提供了_Abs的默认实现

最后,我们将~>调用隐藏在公共包装器方法中。如果T实际上是绝对有价值的,则将选择更专业、因而更高效的~>

这也是我们获得42~>\u advance12或42~>\u distanceTo23的原因,如中所示,因为对于一般ForwardIndexType,.advanceBy和.distanceTo方法是打开的,但如果类型是RandomAccessIndexType,则可以在O1中实现

你不需要~> 此模式也可以在不调用~>操作符的情况下使用协议上的扩展来完成:

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()
}
这就是为什么除abs之外的所有其他~>实现都在Swift 2中消失的原因:所有使用此技术的专门化都已更改为使用更明显的协议扩展,例如

低估 高级的,高级的 等 注意:雷达问题14011860不是公开的,但我们可以通过OpenRadar上的重复项看到缺陷的范围:


此运算符已从Swift 3.0的运算符章节中删除。

因为它是最高优先级,所以它将与对象/可选延迟相关。我不确定您是否可以这样假设。没有为声明后缀运算符!,或我不确定你能不能申报。这些是语言本身的一部分,并充当其他函数调用的语法糖。
// 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 }
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
}
protocol AbsoluteValuable : SignedNumber {
    static func abs(_: Self) -> Self
}

func ~> <T: AbsoluteValuable>(x: T, _: (_Abs, ())) -> T {
    return T.abs(x)
}
func abs<T: SignedNumber>(_ x: T) -> T {
    return x ~> (_Abs(), ())
}
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()
}