什么是Swift中使用的〜>(代码大于)运算符?

Jas*_*son 12 swift

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

infix operator ~> {
    associativity left
    precedence 255
}
Run Code Online (Sandbox Code Playgroud)

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

rin*_*aro 7

看起来,它是相关的集合/序列/索引类型

Defines-Swift说:

protocol SequenceType : _Sequence_Type {
  typealias Generator : GeneratorType
  func generate() -> Generator
  func ~>(_: Self, _: (_UnderestimateCount, ())) -> Int
  func ~><R>(_: Self, _: (_PreprocessingPass, ((Self) -> R))) -> R?
  func ~>(_: Self, _: (_CopyToNativeArrayBuffer, ())) -> _ContiguousArrayBuffer<Self.Generator.Element>
}

protocol CollectionType : _CollectionType, SequenceType {
  subscript (position: Self.Index) -> Self.Generator.Element { get }
  func ~>(_: Self, _: (_CountElements, ())) -> Self.Index.Distance
}

protocol ForwardIndexType : _ForwardIndexType {
  func ~>(start: Self, _: (_Distance, Self)) -> Self.Distance
  func ~>(start: Self, _: (_Advance, Self.Distance)) -> Self
  func ~>(start: Self, _: (_Advance, (Self.Distance, Self))) -> Self
}

protocol SignedNumberType : _SignedNumberType {
  prefix func -(x: Self) -> Self
  func ~>(_: Self, _: (_Abs, ())) -> Self
}

func ~><T : _CollectionType>(x: T, _: (_CountElements, ())) -> T.Index.Distance
func ~><T : _CollectionType>(x: T, _: (_UnderestimateCount, ())) -> Int
func ~><T : _CollectionType, R>(s: T, args: (_PreprocessingPass, ((T) -> R))) -> R?
func ~><T : _SequenceType>(s: T, _: (_UnderestimateCount, ())) -> Int
func ~><T : _SequenceType, R>(s: T, _: (_PreprocessingPass, ((T) -> R))) -> R?
func ~><S : _Sequence_Type>(source: S, _: (_CopyToNativeArrayBuffer, ())) -> _ContiguousArrayBuffer<S.Generator.Element>
func ~><C : CollectionType where C._Element == C._Element>(source: C, _: (_CopyToNativeArrayBuffer, ())) -> _ContiguousArrayBuffer<C._Element>
func ~><T>(x: EmptyCollection<T>, _: (_CountElements, ())) -> Int
func ~><T : _ForwardIndexType>(start: T, rest: (_Distance, T)) -> T.Distance
func ~><T : _ForwardIndexType>(start: T, rest: (_Advance, T.Distance)) -> T
func ~><T : _ForwardIndexType>(start: T, rest: (_Advance, (T.Distance, T))) -> T
func ~><T : _BidirectionalIndexType>(start: T, rest: (_Advance, T.Distance)) -> T
func ~><T : _BidirectionalIndexType>(start: T, rest: (_Advance, (T.Distance, T))) -> T
func ~><T : _RandomAccessIndexType>(start: T, rest: (_Distance, (T))) -> T.Distance
func ~><T : _RandomAccessIndexType>(start: T, rest: (_Advance, (T.Distance))) -> T
func ~><T : _RandomAccessIndexType>(start: T, rest: (_Advance, (T.Distance, T))) -> T
func ~><T : _SignedNumberType>(x: T, _: (_Abs, ())) -> T
func ~><T : AbsoluteValuable>(x: T, _: (_Abs, ())) -> T
func ~><T>(x: CollectionOfOne<T>, _: (_CountElements, ())) -> Int
Run Code Online (Sandbox Code Playgroud)

例如,

42 ~> _advance(12) // -> 54
42 ~> _distanceTo(23) // -> -19
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用这些: - /


ken*_*ytm 7

由于Swift是开源的,我们可以看到包含~>在stdlib中的实际原因:作为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 }
Run Code Online (Sandbox Code Playgroud)

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

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


怎么~>工作

在Swift 2中,唯一剩下的实例~>abs函数(它可能也会消失).我们可以看到它是如何~>工作的.从stdlib/public/core/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
}
Run Code Online (Sandbox Code Playgroud)

这里,箭头的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)
}
Run Code Online (Sandbox Code Playgroud)

接下来,我们有一个子协议,AbsoluteValuable它可能有一种更有效的方法来计算绝对值x < 0 ? -x : x.然后,我们专门为~>运营商AbsoluteValuable.

func abs<T: SignedNumber>(_ x: T) -> T {
    return x ~> (_Abs(), ())
}
Run Code Online (Sandbox Code Playgroud)

最后,我们~>在公共包装器方法中隐藏调用.如果T实际上是a AbsoluteValuable,则将选择更专业且更有效的~>.

这也是为什么我们得到42 ~> _advance(12)42 ~> _distanceTo(23)@ rintaro的答案所示,因为.advanceBy.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()
}
Run Code Online (Sandbox Code Playgroud)

特别是这就是~>除了absSwift 2 之外的所有其他实现的原因:使用这种技术的所有特化都已经改变为使用更明显的协议扩展,例如


注意:雷达问题14011860不公开,但我们可能会在OpenRadar上看到错误的重复范围: