lua-users home
lua-l archive

Re: Operator precedence and unary minus

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 05/22/2018 07:52 AM, Egor Skriptunoff wrote:
> On Tue, May 22, 2018 at 1:57 AM, dyngeccetor8 wrote:
> 
>>
>> Lua 5.3 has four unary operators: "#", "~", "-", "not".
>> Lowering priority of "-" looks inconsistent.
>> Lowering priority of all unary operators hurts more than cures.
>>
>>
> Unary operators are very different.
> Why do you want all unary operators to have the same priority?
> I see no point in it.
> 
I find it easy to state that all unary operators have the same
priority than to explain why unary minus is so different.
By the way, "~ - 1" is "0" now. After you change it'll become
"- ~ 1" which is "2".
Your well explained case with non-symmetric "mod" is one of
features of languages which have to be kept in mind.
Just like platinum question about "broken" "#" operator.
--
I believe all unary operators should have highest priority.
Current operator priorities:
 3.4.8 – Precedence
 Operator precedence in Lua follows the table below, from lower to higher priority:
 or
 and
 < > <= >= ~= ==
 |
 ~
 &
 << >>
 ..
 + -
 * / // %
 unary operators (not # - ~)
 ^
 -- https://www.lua.org/manual/5.3/manual.html
So "- 2 ^ 2" is "-4.0" and to square length of string "s" you
have to write "(# s) ^ 2".
-- Martin

AltStyle によって変換されたページ (->オリジナル) /