Lua Trivia Answers

lua-users home
wiki

Difference (from prior major revision) (minor diff, author diff)

Changed: 3c3
It totally depends on what b actually is!
It depends what a.b is!

Removed: 5d4
If we're given

Changed: 7,11c6,16
b = function(arg) print(arg) end
}}}
then we will see table a's address however if b is a table
{{{!Lua
b.__call = function(arg) print(arg) end
a = {}; b = {}; c = {}
print(a,b,c) --> a,b,c

a.b = function(...) print(...) end
a:b(c) --> a,c

mt = {}
mt.__call = function(...) print(...) end
a.b = setmetatable(b, mt)
a:b(c) --> b,a,c
a.b(a,c) --> b,a,c (identical)

Removed: 13d17
we will see table b's address.

Changed: 15c19
This question/answer is confusing on a number of points (e.g. what "defined in terms of" means, absence of "setmetatable" call, and "a" could have a metatable too). What you're trying to check is that the reader recognizes that given "a:b()", "a.b" can be either a function or a table. Maybe just state the question as follows: "a:b() invokes a single function. What arguments are passed to that function? Answer: it depends whether a.b is a function or a table with __call metamethod." --DavidManura
Furthermore, a, c, and the global environment table could invoke metamethods that call functions too.

Changed: 26,27c30,31
as you would expected if you are a C programmer!
This is simply because the -- sign in the third line is not interpreted as a variable decrement, but as the start of a comment... The ++ operator does not exist in Lua neither, but does not generate an error here because it is inside that comment!
as you would have expected if you are a C programmer!
This is simply because the -- sign in the third line is not interpreted as a variable decrement, but as the start of a comment... The ++ operator does not exist in Lua either, but does not generate an error here because it is inside that comment!

Answer A

It depends what a.b is!

a = {}; b = {}; c = {}
print(a,b,c) --> a,b,c

a.b = function(...) print(...) end
a:b(c) --> a,c

mt = {}
mt.__call = function(...) print(...) end
a.b = setmetatable(b, mt)
a:b(c)   --> b,a,c
a.b(a,c) --> b,a,c (identical)

Furthermore, a, c, and the global environment table could invoke metamethods that call functions too.

Answer B

It prints
2       3       2
and not
1       4       6
as you would have expected if you are a C programmer! This is simply because the -- sign in the third line is not interpreted as a variable decrement, but as the start of a comment... The ++ operator does not exist in Lua either, but does not generate an error here because it is inside that comment!

Note that the lack of syntax highlight in the question was in fact intentional.

Back to LuaTrivia


RecentChanges · preferences
edit · history
Last edited April 13, 2019 8:54 am GMT (diff)