Let’s look at this simple code:
local Mas = {1, 2, 3, 4, 5}
print(#Mas^2) --will error
print((#Mas)^2) --won't error
Why this works like this though? Why power operator has higher priority than length one?
Let’s look at this simple code:
local Mas = {1, 2, 3, 4, 5}
print(#Mas^2) --will error
print((#Mas)^2) --won't error
Why this works like this though? Why power operator has higher priority than length one?
Damn. That um. That sucks. Thanks for the cursed knowledge.
It’s because of the precedence in Lua.
Its kind of complicated, but its something like this
the # returns a number, but the precedence expects a number,
so putting it in brackets groups the length operation first, then applies the power.
Basically, use brackets when you want to sort which operation goes first. Heres another example:
print(10 / (10*10)) -- returns 0.1
print((10*10) / 10) -- returns 10
Lua thinks it’s this
#(Mas^2)
Instead of
(#Mas)^2