[swift-evolution] Proposal: Adding precedence option for prefix and postfix operators
Maximilian Hünenberger
m.huenenberger at me.com
Sun Jan 17 11:17:26 CST 2016
Even though `-3 ** 2` seems to equal 9 but see this result: http://m.wolframalpha.com/input/?i=-3+%5E+2&x=0&y=0
So this behavior forces you to set correct parenthesis around numbers with a minus sign: (-3) ** 2
In order to make -3 ** 2 visually less ambiguous we could allow (or even require) prefix operators to have a white space separator:
- 3 ** 2
Another example:
3 * -4
Would be in this case also ambiguous for the compiler since `*` and `-` have the same precedence and you are forced to write it as:
3 * (-4)
Which is also mathematically more correct
I just noticed that
3 - -4
Is still unambiguous.
If the precedence of prefix - would be the same as infix `+` and `-` all cases I can think of are ambiguous so you are forced to use correct mathematical syntax:
3 - (-4)
Which could also be an indicator to rewrite it to
3 + 4
And 3 + (-4) to 3 - 4
- Maximilian
> Am 17.01.2016 um 17:48 schrieb David Sweeris <davesweeris at mac.com>:
>
> In that context, I would say 9 is the correct answer. Mathematically speaking, the "-" is part of the number, not an operator.
>
> At least I think that's how it worked last time I was in math class.
>
> - Dave Sweeris
>
>> On Jan 17, 2016, at 08:24, Maximilian Hünenberger via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> It's true prefix operators have the highest precedence (like ∞) but it should be lower so `-3**2` gets mathematically correct evaluated to `-9` if such an expression appears.
>>
>> - Maximilian
>>
>>> Am 17.01.2016 um 02:36 schrieb 肇鑫 <owenzx at gmail.com>:
>>>
>>> I think they are already the highest precedence. That why they need not to be set this property.
>>>
>>> Also, I think things like
>>>
>>>> print(-3**2)
>>>> print(0-3**2)
>>>
>>> should never appear in Swift. As it is hard for human to read.
>>>
>>> zhaoxin
>>>
>>>
>>>> On Sun, Jan 17, 2016 at 5:21 AM, Maximilian Hünenberger <swift-evolution at swift.org> wrote:
>>>> +1 for me and as far as values go:
>>>>
>>>> prefix -
>>>> precedence 150, same as infix * since it is essentially (-1)*
>>>>
>>>> prefix +
>>>> same as prefix -
>>>>
>>>>
>>>> To break the least amount of code:
>>>>
>>>> prefix !
>>>> precedence 140, which is higher than any other Bool operator (== is highest with 130)
>>>>
>>>> prefix ~
>>>> precedence 170, which is higher than any other binary operator (<< is highest with 160)
>>>>
>>>> > Am 16.01.2016 um 16:30 schrieb Jason Nielsen via swift-evolution <swift-evolution at swift.org>:
>>>> >
>>>> > Hi all,
>>>> >
>>>> > My proposal is to add a precedence option for prefix and postfix operators. It is great that swift allows for associativity and precedence for binary operators but it hasn't quite gone all the way to make operator overloading fully functional (just an opinion). To illustrate consider the following code:
>>>> >
>>>> > import CoreFoundation
>>>> >
>>>> > infix operator ** { associativity right precedence 200 }
>>>> >
>>>> > func ** (base: Double, power: Double) -> Double {
>>>> > return pow(base, power)
>>>> > }
>>>> >
>>>> > print(-3**2)
>>>> > print(0-3**2)
>>>> >
>>>> > which prints 9 and -9. In the first case because unary minus has higher precedence as a prefix operator it evaluates to (-3)*(-3) and the second because - is viewed as a binary operator of lower precedence as (0-(3*3). Exponentiation has higher precedence than subtraction so -3**2 should be -9 and the two expressions above are mathematically equivalent. I originally reported this as a bug (SR-552) as to me the point of operator overloading is to allow you to write numerical expressions cleanly but should give you the correct mathematical result. The only really useful application I can think of for operator overloading is basically as a DSL for numerical expressions. If it doesn't allow you to get correct results without having to put brackets everywhere it sort of defeats the purpose (just my opinion of course).
>>>> >
>>>> > Best regards,
>>>> > Jason
>>>> > _______________________________________________
>>>> > swift-evolution mailing list
>>>> > swift-evolution at swift.org
>>>> > https://lists.swift.org/mailman/listinfo/swift-evolution
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> _______________________________________________
>> 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/20160117/ad27e1d3/attachment.html>
More information about the swift-evolution
mailing list