Lua Trivia Answers |
|
|
It totally depends on what b actually is! |
It depends what a.b is! |
|
If we're given |
|
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) |
|
we will see table b's address. |
|
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. |
|
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! |
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.
2 3 2
1 4 6
-- 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