What is the ~> (tilde greater than) operator used for in Swift? -


swift 1.1 includes declaration of ~> operator:

infix operator ~> {     associativity left     precedence 255 } 

what used in swift? appears declared no functions defined leverage it. other developers have used reactive patterns , marshaling closures between queues, wondering why it's defined in standard framework. surmise it's there "reserve" custom operator developer use, given has highest precedence possible.

since swift has been open-sourced can see actual reason including ~> in stdlib: as workaround method specialization in child protocol in swift 1.x.

// workaround <rdar://problem/14011860> subtlf: default // implementations in protocols.  library authors should ensure // operator never needs seen end-users.  see // test/prototypes/genericdispatch.swift documented // example of how operator used, , how use can hidden // users. infix operator ~> { associativity left precedence 255 } 

a detailed example can found in test/prototypes/genericdispatch.swift.

note: do not use ~> in swift 2+. historical workaround. no longer needed. read on.


how ~> works

in swift 2, remaining instance of ~> abs function (which going away well). can see how ~> works. stdlib/public/core/integerarithmetic.swift.gyb, signedinteger protocol defines ~> operator:

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 } 

here, rhs of arrow ~> specifies command can sent object. think of p~>(cmd, args) p.cmd(args).

then provide default implementation of _abs when given signednumber.

protocol absolutevaluable : signednumber {     static func abs(_: self) -> self }  func ~> <t: absolutevaluable>(x: t, _: (_abs, ())) -> t {     return t.abs(x) } 

next, have child protocol, absolutevaluable, perhaps has more efficient way compute absolute value x < 0 ? -x : x. specialize ~> operator absolutevaluable.

func abs<t: signednumber>(_ x: t) -> t {     return x ~> (_abs(), ()) } 

finally hide ~> call in public wrapper method. if t absolutevaluable, more specialized , more efficient ~> chosen.

this why 42 ~> _advance(12) or 42 ~> _distanceto(23) shown in @rintaro's answer, because .advanceby , .distanceto methods o(n) general forwardindextype, can implemented in o(1) if type randomaccessindextype.


you don't need ~>

this pattern done without invoking ~> operator, using extensions on protocol:

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)     }     // allow subtypes override     // genericabs() directly, instead of overriding      // static abs(). }  func abs<t: signedinteger>(x: t) -> t {     return x.genericabs() } 

in particular why other ~> implementations besides abs gone in swift 2: specializations using technique has been changed use protocol extensions more obvious, e.g.


note: radar problem 14011860 not public, may see scope of bug duplicates on openradar:


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -