Lua Trivia Answers

lua-users home
wiki

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 9:54 am GMT (diff)