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
Before you make a thread asking for help, read this.
Post Reply
User avatar
IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

table[key] when it's the actual table

Post by IMP1 » Wed Aug 28, 2013 9:54 am

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?

User avatar
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

Post by Robin » Wed Aug 28, 2013 10:20 am

Try this:

Code: Select all

print( ({ [true] = "1", [false] = "2" })[true] )
Help us help you: attach a .love.

User avatar
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

Post by ejmr » Thu Aug 29, 2013 3:37 am

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
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經

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

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

Post by micha » Thu Aug 29, 2013 5:39 am

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

User avatar
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

Post by ejmr » Thu Aug 29, 2013 6:05 am

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
南無妙法蓮華經

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post by kikito » Thu Aug 29, 2013 10:56 am

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.
When I write def I mean function.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 15 guests