什么是~>;(波浪号大于)Swift中使用的运算符?
Swift 1.1包括~>运营商的声明:什么是~>;(波浪号大于)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:
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中的实际原因:
这里,箭头~>
的右侧指定可以向对象发送的命令。把p~>(cmd,args)
想象成类似于p.cmd(args)
然后,当给定一个签名编号时,我们提供了默认的\u Abs
protocol AbsoluteValuable : SignedNumber {
static func abs(_: Self) -> Self
}
func ~> <T: AbsoluteValuable>(x: T, _: (_Abs, ())) -> T {
return T.abs(x)
}
最后,我们将~>
调用隐藏在公共包装器方法中。如果T
实际上是一个绝对有价值的
,则将选择更专业、因而更高效的~>
这也是为什么我们得到如中所示的42~>\u advance(12)
或42~>\u distanceTo(23)
,因为.advanceBy
和.distanceTo
方法对于一般的ForwardIndexType
是O(n),但如果类型是RandomAccessIndexType
,则可以在O(1)中实现
您不需要
~>
此模式也可以在不调用~>
操作符的情况下使用协议上的扩展来完成:
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()
}
协议签名集成:可比较{
前缀func-(uu2;:Self)->Self
func genericAbs()->Self
}
扩展签名整数{
func genericAbs()->Self{
返回self<0?-self:self
}
}
协议绝对值:SignedInteger{
静态函数abs(uu:Self)->Self
}
扩展绝对值{
func genericAbs()->Self{
返回Self.abs(Self)
}
//通常,您会允许子类型覆盖
//genericAbs()直接,而不是重写
//静态abs()。
}
函数abs(x:T)->T{
return x.genericAbs()
}
特别是,这就是为什么除了abs
之外的所有其他~>
实现都在Swift 2中消失的原因:所有使用此技术的专业化都已更改为使用更明显的协议扩展,例如
in中的计数不足
,advancedBy
indistance to
- 等等
注意:雷达问题14011860不是公开的,但我们可以通过OpenRadar上的重复项看到缺陷的范围:
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()
}