[swift-evolution] Proposal: Adding precedence option for prefix and postfix operators
Jeremy Pereira
jeremy.j.pereira at googlemail.com
Wed Jan 20 03:45:57 CST 2016
> On 20 Jan 2016, at 07:48, Jason Nielsen <drjdnielsen at gmail.com> wrote:
>
> That is the case because brackets have higher precedence... these are the rules that you leaned in primary school (BEDMAS -- Brackets, exponentiation, division, multiplication, addition and subtraction). So (-x)^n is expressed as (-x)*(-x)*....*(-x) n times (if n is even then positive, if odd negative). By the same logic -x^n is -(x*x*...*x). Swift doesn't consider spacing important in terms of precedence else 2+3 * 4 would equal 20 in which case I wouldn't have even made the original bug report or started this e-mail. There is no concept of spacing in expressing an equation in math. Again having negation (using computer science speak unary minus aka the prefix operator - ) having precedence over any other operator by default is a mistake and limits the utility of operator overloading in the language.
But here you are arguing a case only for exponentiation - which doesn’t exist natively in Swift - and the unary minus operator.
It’s also not true to say spacing is not important in Swift. As I said
-3**-2
doesn’t even compile. Yes, putting spaces in does not alter precedence, but if you do
-3 ** -2
it *looks* like the unary minus has higher precedence, and it does today.
What about other unary operators. How do you want the precedence of
2 ** myExponent!
to go?
Swift today has a fairly simple rule which appears to fall down only in one case, where the unary operator is minus and the binary operator is exponentiation (which is something you’d have to define). I don’t think the additional complexity is worth it.
More information about the swift-evolution
mailing list