[swift-evolution] [Idea] Allow more operators as custom operators

Антон Жилин antonyzhilin at gmail.com
Sun Apr 10 08:01:19 CDT 2016


& (as a prefix operator), ->, ?, and ! (as a postfix operator)

This is the list of built-ins that look like operators, but are banned from
use as Swift custom operators.

We can review that list.

`&` reserved as a prefix operators for a reason. It marks a variable use as
`inout`, which currently cannot be done by any Swift operator function.

Other three don't have such justification.

`->`, `?` and `!` are used in types, but they are mostly unambiguous in
expressions.

The only use of `!` in expressions can be rewitten as a built-in operator
function:
postfix func ! <T> (left: T!) -> T

`?` is used in optional method calls:
a.method?(b)
A parallel proposal is going to remove such syntax from Swift, so this will
not be a problem.

`?` is also used in patterns:
if case x? = optional { ... }

Such use is also unambigious here, as expressions are not allowed in
patterns.

So my proposal is to allow `->`, `?` and potfix `!` as custom operators.
This proposal requires proposal of removing optional protocol requirements.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160410/8dbc0f84/attachment-0001.html>


More information about the swift-evolution mailing list