[swift-evolution] [Proposal] Custom operators
Антон Жилин
antonyzhilin at gmail.com
Thu Apr 7 15:39:12 CDT 2016
First of all, sorry for the delay. I still hope to finish the discussion
and push the proposal to review for Swift 3.0.
Link for newcomers:
https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md
Sadly, I've moved into the territory opposite to what I had in mind in the
beginning: absense of conflict resolution.
I wanted lightweight directives, but am moving to closed precedence groups.
It's just IMHO, and I think I just need input on this from more people. I
still have not heard anything from Core team.
My main question would be: maximally "mergeable" directives or closed
declarations?
Now, another iteration of syntax discussion.
I've created a sample of definitions of precedence operators for standard
library, using precedence groups (at the end of the proposal).
There is no evidence that an operator could logically belong to multiple
precedence groups.
Really, precedence groups are just a way of writing multiple identical
operator declarations in a shorter way.
Precedence groups will be closed, meaning that no precedence relations can
be added to them outside of their bodies.
It will make merging all standard library operators in a giant hierarchy
less tempting, although possible.
Still, that would be possible. We could disallow that specifically as a
future direction (only for standard library).
Still, new operators must be able to be added to existing precedence groups.
Extensions cannot be used, because namespaces of types and precedencegroups
will not intersect.
So I have to return to declaration of operators, if noone finds a better
way and if noone objects:
precedencegroup Additive {
members(+, -)
associativity(left)
precedence(> Comparative)
}
infix operator +
infix operator -
infix operator &+ { precedencegroup(Additive) }
All operators must have precedence groups.
I thought of allowing operators to be single-operator precedence groups,
but it wouldn't give any real benefits.
I also thought of allowing operators without precedence, but almost all
operators will want at least `precedence(>Assignment)`.
Now, what questions did arise from standard library operator declarations?
1. All precedence groups have a "parent".
It means, all operators will want to have precedence higher than
Comparative or Ternary, or, at least, Assignment.
2. Moreover, I could not find any case where I had to write anything other
than precedence(>, ...)
Of cause, I cheated, because I can control all these declarations.
Mere people will have to use `<` to say that Additive, for example, should
have less priority than their custom operator.
But... can you build a custom operator where `<` will actually be needed? I
have even stronger doubts on `=`.
Maybe we can even contract this feature to `parent(Comparative)` or
something without losing any expressivity?
3. Can we allow operators to have less priority than `=`?
If yes, can you give an example of such operator?
4. Operators `is`, `as`, `as?`, `as!`, `?:`, `=` are not proper Swift
operators.
But we can still support these tokens for consistency.
Their only appearence would be in the standard library.
Alternatively, we can hide their precedence groups and make them a special
case.
It's more a question of implementation complexity.
5. I removed associativity from Ternary, removed BitwiseXor from bitwise
hierarchy.
And made numerous other changes that probably need to be reviewed.
2016-04-06 9:17 GMT+03:00 Maximilian Hünenberger <m.huenenberger at me.com>:
>
>
> Am 05.04.2016 um 22:32 schrieb Антон Жилин <antonyzhilin at gmail.com>:
>
> Added
> <https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md#use-precedence-groups>
> group version, "lessThan" problem can be solved nicely. `<`, `=`, `>` signs
> would be allowed there.
>
> > Should we allow "precedence(... equalTo ...)" for operators if we have
> precedence groups?
> I think no.
>
> I have a question to your group syntax.
> Since all operators in a precedence group must have equal associativity
> for parsing to work and look logically (right?), wouldn't it be better to
> declare associativity in groups?
> If so, then body of operator declaration won't contain anything, and we
> can remove it:
>
> precedenceGroup Additive {
> associativity(left)
> +, -
> }
> infix operator +
> infix operator -
>
> Does this body of precedenceGroup look OK from syntactic PoV?
>
>
> Associativity in precedence groups is fine however the operators should
> then be grouped possibly: "operators(+, -)"
>
>
> Now, I have another idea.
> As operator declarations themselves don't contain anything anymore, remove
> operator declarations at all. We don't need to pre-declare function names,
> for example.
> Next, `precedenceGroup` could be as well replaced with `precedenceLevel`,
> or just `precedence`, and I would not worry about additional keywords.
> So, our example would look like this:
>
> precedence Additive {
> associativity(left)
> +, -
> }
> precedence Multiplicative {
> associativity(left)
> *, /
> }
> precedence(Additive < Multiplicative)
>
> As a future direction, we could add extensions to precedence levels.
> We could go further and replace `precedence` with `operator`, abandoning
> the idea of priority for prefix and postfix operators (that I honestly
> don't like).
>
>
> Regarding pre- and postfix operators: there was a separate thread which
> discussed exactly this. The biggest problem was that if a prefix "-" has
> lower precedence than an infix operator like "^" this calculation is
> ambiguous from a human perspective:
>
> -3 ^ 3
>
> "-" has visually the higher precedence and the result would be 9. However
> the actual result is -9.
>
> If we have precedence on pre- and postfix operators we would break
> existing code. A migratory could then enforce the old precedence levels
> with braces. But then we can resolve existing (visual) ambiguities and
> "mathematical incorrectness" by making the precedence of prefix "-" higher
> than the current comparative operators and not declare its precedence to
> higher precedence operators (precedence > 140)
>
> Such that this expression is ambiguous to the compiler:
>
> 3 - -3 // also mathematically incorrect
> // and should be rewritten to
> 3 - (-3)
> // or just
> 3 + 3
>
> infix operator Additive {
> members(+, -)
> associativity(left)
> }
> infix operator Multiplicative {
> members(*, /)
> associativity(left)
> precedence(> Additive)
> }
>
> Some other questions:
> Do we need transitive precedence propagation?
>
>
> Yes because it would be quite a pain to declare every precedence between
> all precedence groups:
> #needed precedence declarations ~ O(#of precedence groups ^ 2)
>
> Do we need resolution of conflicts, i.e. merging multiple definitions of
> same operators and groups, where possible?
>
>
> I think we shouldn't define operators in a precedence group because if we
> want to have an operator in two different groups then we have two operator
> definitions which can result in a conflict.
> I'm not sure if we need the same operator in different groups. Therefore
> I'd suggest to declare all standard library operators in this form in order
> to see if we need this.
>
> So my current syntax suggestion is:
>
> infix operator + { associativity(left) }
> prefix operator -
> infix operator && { associativity(left) }
>
> infix precedenceGroup Additive {
> associativity(left)
> members(+)
> }
>
> infix precedenceGroup Logical {
> associativity(left)
> members(&&)
> }
>
> prefix precedenceGroup Sign {
> members(-)
> }
>
> precedence(Additive > Logical)
> precedence(Sign > Logical)
>
> // warning: duplicate precedence declarations
> precedence(Logical < Additive)
>
> --------
>
> I declare associativity in operator declarations and precedence group
> declarations since it lets the compiler check whether the "members" have
> the right associativity.
>
> Best regards
> - Maximilian
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/60f8a801/attachment.html>
More information about the swift-evolution
mailing list