[swift-evolution] [Proposal] Custom operators

Daniel Duan daniel at duan.org
Mon Apr 4 13:21:47 CDT 2016

> Антон Жилин via swift-evolution <swift-evolution at ...> writes:

> Swift 2.2 is out, and I restart discussion on syntax for custom operators.
> I insist that this time we should focus less on linguistic aspects.

I have a few thoughts and a question regarding precedence:

1.  I strongly agree that the numeric precedence system is not great. From
    a implementation point of view, the way to specify them in your proposal
    essentially gave all visible operators a partial order, in which we can
    draw a directed gragh with operators being the nodes and their relation
    being arcs. A part of the graph might look like: '^' --> '*' --> '+', the
    nodes being the math operators. We can tell '*' has a higher precedence
    than '+', '^' has a higher precedence than '*' and '+', by follwing the
    arcs. If one operator is not reachable from another, and vice versa, then
    composing these two is illegal. We need to teach the compiler this concept.

2.  Currently, it's not possible to specify precedence for pre- and postfix
    operators. Chris Lattner has mentioned that the
    following result is not desirable:

        ∆x + y

    … where ∆ has a lower precendence than + while it's required to have no
    space between ∆ and the operand. My understanding is that if spaces were
    to be allowed here, parsing such expression without ambiguity is a
    non-trivial challenge. So, will it be possible to specify precedence for
    pre/postfix operators under your proposal?

3.  It may be a good exercise to work out how would each of the builtin
    operators would be defined with this change and mention it (not the entire
    definition, but the fact that it's possible, or reasons why it produces
    any difference) in the proposal.

- Daniel

More information about the swift-evolution mailing list