Page 1 of 1

table[key] when it's the actual table

Posted: Wed Aug 28, 2013 9:54 am
by IMP1
So why can I do this:

Code: Select all

local b = true
local t = { [true] = "1", [false] = "2" }
print(t[b])
But not this:

Code: Select all

print( { [true] = "1", [false] = "2" }[true] )
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?

Re: table[key] when it's the actual table

Posted: Wed Aug 28, 2013 10:20 am
by Robin
Try this:

Code: Select all

print( ({ [true] = "1", [false] = "2" })[true] )

Re: table[key] when it's the actual table

Posted: Thu Aug 29, 2013 3:37 am
by ejmr
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?
Lua allows you to omit the parentheses in a function call if:
  • You only give the function one argument.
  • That argument is a literal table of string.
For example:

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}
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]
And that code is in error because for that shortcut Lua allows only a literal table constructor and nothing else. Attempting to immediately access a value from the literal table counts as ‘something else’ and so Lua rejects it, because now we are calling the function with something more than just a literal table constructor. Robin’s suggestion works because Lua allows ‘expressions’ as arguments, and wrapping a literal table in parentheses creates an expression, and Lua does lets us immediately lookup a table field from such an expression.

My apologies if the explanation isn’t clear. I just thought it may be helpful to try to explain why Robin’s suggestion is correct. The official documentation for the grammar of function calls will also help explain it:

http://www.lua.org/manual/5.2/manual.html#3.4.9

Re: table[key] when it's the actual table

Posted: Thu Aug 29, 2013 5:39 am
by micha
ejmr 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 disagree here. In the Reference Manual it says
A call of the form f{fields} is syntactic sugar for f({fields});
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].

Re: table[key] when it's the actual table

Posted: Thu Aug 29, 2013 6:05 am
by ejmr
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].
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.

Re: table[key] when it's the actual table

Posted: Thu Aug 29, 2013 10:56 am
by kikito
For the record, I only use the shortcut version for require. For anything else, I use the parenthesis notation - even when it's a function taking one table.