Lua "requires" with global "local"?

It’s clear that I was a bit confused, but I thought that using something like this in main.lua:

local module = require("module") local var = "I should be global?" printthis() 

with module.lua containing something like:

 function printthis() print(var) end 

that printthis(var) will work fine, because now the module.lua code is inside main.lua, no? Instead, printthis has no idea what var . I read that it’s good practice to use “local” by Lua variables if possible, but in this case I need to make var global or is there a way for module.lua printthis() to read var correctly?

+6
source share
3 answers

Not. This is not at all how it works.

The Lua interpreter provides one global table, usually called _G, unless you are doing something strange.

 function printthis() print(var) end 

This means that in reality

 _G.printthis = function() _G.print(_G.var); end 

And your code is basically equal

 local module = _G.require("module") local var = "I should be global?" _G.printthis() 

But when you call printthis - where did _G.var set up? Nowhere. Thus, the nil variable, like all other table calls, where there is nothing in this key.

This may be inconvenient, but in the long run it is a much better idea to pass arguments than instead of global variables. As soon as you come to change something about the program, it is completely impossible to make any changes, since the logic has no structure, and you can not imagine what happens when you do not read every line of code and do not understand it immediately. In addition, you can use only each key in one place, because it is a global table, so I am sure that no one wants to use "var" as a variable name, and you do not mind that your code is silent, because you got the global name is incorrect.

+22
source

The other two answers mask an important thing: a lexical definition.

This means that, roughly speaking, this code can access local variables that are defined where the code is defined. It probably sounds vague, so I'll give an example:

 local cheese = 'sandwich' print(cheese) -- prints sandwich do -- this begins an anonymous block local cheese = 22 print(cheese) -- prints 22 end print(cheese) -- prints sandwich 

So, here we have two different variables: the outer one is “obscured” by the inner one.

Now on the function:

 do local hotdog = 'hot' function nonsense() print(hotdog) end end print(hotdog) -- prints nil, since there is no variable called hotdog here nonsense() -- prints hot 

Functions can see local variables from which they are defined, and not from where they are called. This is very important and very useful as soon as you hang it.

+4
source

I am not an expert in lua, but should not be passed as a variable to var . Something like that:

 function printthis(var) print(var) end 

The function header is missing your var . And you pass var to main.lua as an argument to the printthis() function.

+1
source

Source: https://habr.com/ru/post/892059/


All Articles