Code: Select all
local b = true
local t = { [true] = "1", [false] = "2" }
print(t[b])
Code: Select all
print( { [true] = "1", [false] = "2" }[true] )
Code: Select all
local b = true
local t = { [true] = "1", [false] = "2" }
print(t[b])
Code: Select all
print( { [true] = "1", [false] = "2" }[true] )
Code: Select all
print( ({ [true] = "1", [false] = "2" })[true] )
Lua allows you to omit the parentheses in a function call if:IMP1 wrote:I can replace the b with the value it holds, but I can't replace t with the value it holds? Is this just a feature of tables in lua?
Code: Select all
function sum(numbers)
local total = 0
for _,number in ipairs(numbers) do
total = total + number
end
return total
end
-- These both return the same value:
local foo = sum({1, 2, 3, 4, 5})
local bar = sum {1 ,2, 3, 4, 5}
Code: Select all
-- It sees this...
print( { [true] = "1", [false] = "2" }[true] )
-- ...and treats it like this.
print { [true] = "1", [false] = "2" }[true]
I disagree here. In the Reference Manual it saysejmr wrote:Therefore Lua does this with your code:
Code: Select all
-- It sees this... print( { [true] = "1", [false] = "2" }[true] ) -- ...and treats it like this. print { [true] = "1", [false] = "2" }[true]
I understand that as "the formulation with parentheses is the standard formulation. The one without parenthesis is a short formulation (for convenience) which is interpreted in the same way".A call of the form f{fields} is syntactic sugar for f({fields});
You are correct. After re-reading my post I can see how it sounds like I am saying the shortcut is the norm and not the exception, which is not what Lua does. Thanks for pointing out my mistake.micha wrote: I understand that as "the formulation with parentheses is the standard formulation. The one without parenthesis is a short formulation (for convenience) which is interpreted in the same way".
In my understanding the parenthesis, put by Robin, force the interpreter to evaluate the thing inside (the table constructor) instead of trying to make sense of the whole expressen {...}.[true].
Users browsing this forum: Google [Bot] and 83 guests