[swift-evolution] Allowing Characters for use as Custom Operators

Greg Parker gparker at apple.com
Fri Jan 8 00:52:52 CST 2016


> On Jan 7, 2016, at 9:49 PM, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Some other languages provide special syntax to use a binary function as infix:
> 
> Haskell:
>     foo a b    -- is equivalent to
>     a `foo` b
> 
> Mathematica:
>     Foo[a, b]  (*is equivalent to*)
>     a~Foo~b

Swift's features make that solution trickier: free functions versus methods; named parameters.

Alternative: Reserve one of the operator characters as an operator introducer. Everything from that character to the next whitespace is an operator name. This would allow non-operator characters in operator names while still preserving the strict operator/identifier separation.

    // • is the operator introducer character
    infix operator •times …
    infix operator •mod …
    x = a •times b •mod 8

Limitations:
You still can't use an unadorned word as an operator name.
You can't use such an operator without whitespace (unlike operators whose names use operator characters only). 


-- 
Greg Parker     gparker at apple.com     Runtime Wrangler




More information about the swift-evolution mailing list