Add F#-style pipe-forward (|>) operator and auto-complete to Swift Standard Library and Xcode

Originator:kristopherdjohnson
Number:rdar://17207279 Date Originated:06-Jun-2014 06:36 PM
Status:Open Resolved:
Product:Developer Tools Product Version:6
Classification:Usability/UI Reproducible:N/A
 
F# provides a standard binary operator |>, the "pipe-forward" operator, which applies the function on its right-hand side to the value on its left-hand side. The operator can be defined in Swift like this:

    operator infix |> { associativity left }
    func |> <T,U>(lhs : T, rhs : T -> U) -> U {
        return rhs(lhs);
    }

This operator provides benefits both in readability and in IDE autocompletion support. For example, one can write an expression like this:

    let rows = resultCollection |>  filterBy(selectedCityName) |> orderBy(selectedColumn)

instead of in traditional inside-out/right-to-left function application notation, which would be this:

    let rows = orderBy(selectedColumn)(filterBy(selectedCityName)(resultCollection))

The left-to-right version would be more readable to most people.

When the user types the argument first, and then the |> operator, Xcode could then determine which functions are available that can be applied to an argument of that type and provide an appropriate autocompletion list. This is helpful.

This operator was created for F#, but is also in the Ocaml Batteries Included library and used elsewhere. If Swift provides such an operator, it would be nice to use the same notation used in other languages.

See https://gist.github.com/kristopherjohnson/ed97acf0bbe0013df8af for additional examples and commentary.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!