1

At the begining of some lua package files, sometimes there will be the line local base = _G or local base = ....

  1. What's the benefits for doing this?
  2. What's the differences between these two lines?
Z.H.
  • 259
  • 1
  • 14

1 Answers1

1

For the first question, you can refer: Why make global Lua functions local?

For your second one,

What's the differences between these two lines?

When you do local base = _G, you are assigning base to be a synonym for the global environment table. On the other hand, in the statement local base = ...; the ... refer to vararg feature of lua.

It can be shown in better detail with the following example:

local a = {...}
for k, v in pairs(a) do print(k, v) end

and then, executing it as follows:

─$ lua temp.lua some thing is passed "here within quotes"
1       some
2       thing
3       is
4       passed
5       here within quotes

As you see, ... is just a list of arguments passed to the program. Now, when you have

local base = ...

lua assigns the first argument to the variable base. All other parameters will be ignored in the above statement.

Community
  • 1
  • 1
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • So with the second statement one need to pass `_G` as the argument when loading the package file. Is it right? – Z.H. Oct 12 '15 at 04:03