## table[key] when it's the actual table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

### table[key] when it's the actual table

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?

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Try this:

Code: Select all

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

ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

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

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
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
ejmr :: Programming and Game-Dev Blog, GitHub

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

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].

ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

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

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.
ejmr :: Programming and Game-Dev Blog, GitHub

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm