[swift-evolution] [Proposal] Custom operators
Антон Жилин
antonyzhilin at gmail.com
Mon Apr 4 05:41:22 CDT 2016
Just to clarify, with currently proposed
behaviour, corresponding expressions would be parsed as follows:
a + b * c // a + (b * c)
a * b ^ c // a * (b ^ c)
a ^ b + c // a ^ (b + c)
The compiler would look only at explicitly stated precedence between two
given operators and would not try to use transcendency.
Having said that, at least two opinions were that operators should form a
DAG by precedence. If no objections are stated, I will change the proposal
accordingly, also without future directions.
On, Apr 4, 2016, Ross O'Brien wrote:
> How is a non-transitive relation different from a conflict?
> If we use:
> #precedence(+, lessThan: *)
> #precedence(*, lessThan: ^)
> #precedence(^, lessThan: +)
> Surely that should be an error, even with none of the operators actually
> used?
>
> The compiler would be asked to verify two things: that the relationships
> of all operators in the program can be expressed with a topological
> ordering, and any pair of operators used together in an expression in the
> program has an unambiguous precedence in that ordering. (I have no idea how
> relatable this comparison actually is, but it seems like it would be a
> simpler problem than analysing NSLayoutConstraints for violations.)
>
> On Mon, Apr 4, 2016 at 7:49 AM, Антон Жилин <swift-evolution at swift.org>
> wrote:
>
>> In the poposed model, all relations are not transitive. Example:
>>
>> #precedence(+, lessThan: *)
>> #precedence(*, lessThan: ^)
>> 1 ^ 2 + 3 // error
>> #precedence(+, lessThan: ^)
>> 1 ^ 2 + 3 // now ok
>>
>> Would it be better to have such indirect relations inferred? Or would it
>> put too much responsibility on the compiler?
>> Maybe add it to future directions?
>>
>> Cycles of length >2 are also allowed. This was not added intentionally,
>> but follows from other specified rules.
>> Example: ^ (binary) < & (binary) < + < * < ^ (power). OK, & < + is a bit
>> stretched, otherwise quite logical.
>>
>> > - I wonder if there are cases in the standard operators which would be
>> better modeled as a non-linear chain.
>> In the standard library, non-linearity will be primarily used to break a
>> single hierarchy into multiple small ones. I doubt that any trees or cycles
>> will form, although that would be good news.
>>
>> - Anton
>>
>> 2016-04-04 8:55 GMT+03:00 David Waite <david at alkaline-solutions.com>:
>>
>>> Interesting model!
>>>
>>> If I understand correctly: this changes the precedence from being based
>>> on a numeric value, to being represented as a bit of a DAG of precedence
>>> groups. A precedence group is defined implicitly for each operator, with
>>> one group around each set of operators where equalTo has been declared.
>>>
>>> The groups are lazily evaluated, so if an expression can be resolved
>>> without ambiguity due to lack of reachability between two individual
>>> operators in the DAG, there is no issue/error.
>>>
>>> Comments:
>>> - I wonder if there are cases in the standard operators which would be
>>> better modeled as a non-linear chain. The compiler could warn around usage
>>> which is defined by operator precedence, but is commonly considered
>>> ambiguous or error prone.
>>>
>>> For example, if users commonly get confused about the conjunctive and
>>> disjunctive levels (logical ‘and’ and ‘or’) being different levels with
>>> precedence, you could just omit the lessThan relationship between the two
>>> of them. The compiler would then error on ambiguous cases, prompting the
>>> user to use parenthesis.
>>>
>>> - I’d prefer instead of operator precedence groups just being implicit
>>> by use of #precedence equalTo, that the operators are bound to a group
>>> explicitly. Something like
>>> #precedence(+, group: “additive”)
>>> #precedence(“additive”, lessThan: “multiplicative”)
>>>
>>> However, this may create more issues than it solves (two frameworks
>>> creating their own custom operators, putting them in custom precedence
>>> groups, and the consumer decides the two precedence groups are really
>>> equivalent)
>>>
>>> -DW
>>>
>>> On Apr 3, 2016, at 3:36 AM, Антон Жилин via swift-evolution <
>>> swift-evolution at swift.org> wrote:
>>>
>>> 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.
>>>
>>>
>>> https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md
>>>
>>>
>>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160404/94de8511/attachment.html>
More information about the swift-evolution
mailing list