Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation about “local foo = foo” idiom in Lua

Tags:

lua

In Programming in Lua (3rd Ed.) by Roberto Ierusalimschy it is stated that

A common idiom in Lua is

local foo = foo

This code creates a local variable, foo, and initializes it with the value of the global variable foo. (The local foo becomes visible only after its declaration.) This idiom is useful when the chunk needs to preserve the original value of foo even if later some other function changes the value of the global foo; it also speeds up the access to foo.

Could someone explain this more in detail and provide a simple example?

At the moment, the only use I can think of for this idiom is to manage local variables (in a given block) that have the same names as global variables, so that the global variable is left unchanged after the block.

An example:

foo = 10
do
   local foo = foo
   foo = math.log10(foo)
   print(foo)
end
print(foo)

this gives:

1
10

But the same could be accomplished without using the idiom at all:

bar = 10
do
   local bar = math.log10(bar)
   print(bar)
end
print(bar)

that gives the same result. So my explanation doesn't seem to hold.

like image 669
Pier Paolo Avatar asked Feb 12 '15 23:02

Pier Paolo


People also ask

What does local Do in Lua?

Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones. Lua handles local variable declarations as statements. As such, you can write local declarations anywhere you can write a statement.

What does _G do in Lua?

Lua allows you to customize the behavior of tables (and other values) by setting a metatable using the setmetatable function, but you have to pass the table as a parameter somehow. _G helps you do that.

What does global mean in Lua?

Global variables in Lua are the variables that don't need any type of declaration in them. We can simply define the name of the variable and assign any value we want to it, without having to use any keyword with it.


4 Answers

I've seen this used more often as a optimization technique than as a way to preserve original values. With the standard Lua interpreter, every global variable access and module access requires a table lookup. Local variables, on the other hand, have statically-known locations at bytecode-compile time and can be placed in VM registers.

In more depth: Why are local variables accessed faster than global variables in lua?

like image 118
Wilbur Vandrsmith Avatar answered Oct 03 '22 13:10

Wilbur Vandrsmith


The explanation is correct; I'm not sure why you are not satisfied with your example. To give you a real example:

local setfenv = setfenv
if not setfenv then -- Lua 5.2+
  setfenv = function() ..... end
end

Another reason is to preserve the value as it is at this moment, so that other functions that use that value (in a file or a module) would have the same expectations about that value.

like image 39
Paul Kulchenko Avatar answered Oct 02 '22 13:10

Paul Kulchenko


Wrapping a global:

do
  local setmetatable = setmetatable
  function _ENV.setmetatable(...)
    -- Do your thing
    return setmetatable(...)
  end
end

Reducing overhead by using a local instead of doing a lookup in the globals-table (which is a local btw.):

local type = type
for k, v in next, bigtable do
  if type(v) == "string" then
    -- Do one thing
  else
    -- Do other thing
  end
end
like image 24
Deduplicator Avatar answered Oct 01 '22 13:10

Deduplicator


I think you're splitting hairs, unintentionally.

local bar = math.log10(bar)

is essentially the same as local bar = bar in spirit, but we it would be less useful to claim that the idiom is local bar = a(bar), because we may want to handle the local in some way other than passing it to a function first -- e.g. appending it to something.

This point is that we want to refer to the local bar, just as you say, not exactly how the conversion from global to local is done.

like image 34
bcr Avatar answered Oct 01 '22 13:10

bcr