Understanding Scope: A Blog Post

I’ve noticed in several forum posts that some people struggle with the concept of variable and function scope.

It’s an important topic for all programmers to understand, and one that comes with experience, but its a topic that can frustrate new programmers, so I decided to write a blog post about the topic:

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

Enjoy!
Rob [import]uid: 19626 topic_id: 16401 reply_id: 316401[/import]

V nicely done. Would you like to guess blog post it on our ansca blog?

Let me know at cicaza [att] anscamobile [dot] com or hetal [att] anscamobile [dot] com

c [import]uid: 24 topic_id: 16401 reply_id: 61238[/import]

This is a FANTASTIC post Rob - I am sure it will help a lot of new users. (I have bookmarked it to recommend in the future.)

Peach :slight_smile: [import]uid: 52491 topic_id: 16401 reply_id: 61241[/import]

Hello Rob!

Just stopped here because I must say: “Thank You”! :]

PS: I am a child in this Corona/Lua World and SO blog post like that you wrote, oh my friend, HELPS A LOT us the beginners!
Sincerely,

My best regards of looking forward for the less experts as myself.
Rodrigo. [import]uid: 89165 topic_id: 16401 reply_id: 61271[/import]

Hi Rob, I finally got around to read your blog post. I really appreciate you sharing it with us. There were a couple of things I wasn’t quite sure, which you helped to clarify for me:

  1. Use of _G. It sounds like _G is provided by Lua to help coders/programmers to remember what is declared as global and what isn’t (meaning, if it’s not _G.myVariable, then it should be declared local the first time the variable is introduced). I avoided using _G because I didn’t have a feel for it, and I’d rather use what I know I’m using. So, in place of _G, I declared one variable global (say, for example, myGlobal, which is a table), and use it in place of _G. I suppose it’s okay, but is there a reason why I should replace myGlobal with _G?

  2. Use of local. I think I missread somewhere that if I declare a variable local and then declare it to be local again, it turns into a global variable. Your blog post completely eradicated my misconception. It will definitely make it easier for me to declare things local. I love this. I was taking trouble creating different names for each local variable, and double/tripple check to make sure local isn’t infront of the variable more than once in each lua module. Now, I can safely use same name with local in front of it without too much worries, so long as they are not inside the same namespace.

Now, I have a couple of questions.

Q1) I’m thinking that once a project is launched, until I exit the project, main.lua is never unrequired, and therefore, it’s in memory. Would that make local variables in main.lua accessible to other lua module, especially when other modules use module(…, package.seeall);? From what you wrote, I now think that all variables declared as local in main.lua are only available in main.lua. Even if other module can see main.lua, that doesn’t make what’s local to main.lua accessible to the other module. Just wanted to make sure I understood this.

Q2) I’ve been puzzling over why would anyone do something like:

[lua]local director = require(“director”);[/lua]

I use director class, and it is needed in every lua scene. I don’t understand why I would want to declare it as local in main.lua (and if I do, I would need to require director locally in every lua scene I load). What I decided was to require classes that I know I’d use almost everywhere as global. So, my main.lua requires classes like this:

[lua]-- require as global
diretor = require(“director”);
ui = require(“ui”);
loqsprite = require(“loq_sprite”);
loq_util = require(“loq_util”);
json = require(“json”);
physics = require(“physics”);
store = require(“store”); – not needed everywhere, but I think it needs to be global in my case[/lua]

Is it better to require them as local (except the “store” I suppose), and require them in each module that I need them? If it is better to do so, what might be the reason? Why should I not require them as global?

Thanks again for helping people new to programming!

[import]uid: 67217 topic_id: 16401 reply_id: 61615[/import]

Using myGlobals and _G should be pretty darn equal, unless there are things about Lua that I don’t understand (and I’ll admit, I’ve only been using for about 6 months and I’m not an expert…). I have been programming for 30 years. I’ll claim expertise there :slight_smile: :slight_smile: :slight_smile:

_G is there by the OS for that purpose and its convention to use it, which would be the only reason I would suggest it. The _ prefix is to avoid anyone who uses G as a variable for a group (you see it in a lot of sample code) and where someone forgets to make G local. People rarely prefix variables with _'s, but it is a convention in other languages to use one to three _'s in front of private variables that end up in global space to avoid naming conflicts.

Q1. I ran a quick test. I created a module called variables.lua:

module(..., package.seeall)  
  
function display()  
 print(variableA) -- outputs "fred"  
 print(variableB) -- outputs nil  
 print("-----------------")  
end  
  

and in main.lua I have:

local variables = require("variables");  
  
variableA = "fred";  
local variableB = "barney";  
  
variables.display()  

My output is:

fred
nil
-----------------

Even with seeall, the module was unable to see variableB, so I can deduce from there that local variables in the main.lua chunk are maintained local to that chunk. package.seeall causes the programs global space to be made available to that module. In other words, if I left that out, I wouldn’t get my global variables (and I won’t get other global things, like “print” and other functions!!!)

Q2: Someone smarter than me will need to provide the definitive answer on this, but in the apps where I’m using director, I include it in main.lua

local director = require("director")  

but in my other lua scene files, I don’t include that require again. So it appears that loaded modules end up being global anyway.

Someone smarter than me please clarify this bit for us! [import]uid: 19626 topic_id: 16401 reply_id: 61629[/import]

Thank you, Rob, for such a detailed response with test case too! It’s so nice of you.

It makes it confusing that:

  1. local variable introduced in main.lua remains local to main.lua, and other module (even with seeall) cannot see it… while…

  2. locally required module (like director) in main.lua does not remain local to main.lua, but instead, becomes global.

It feels like a conflicting logic. I suppose there are rules in Lua that we are not aware of. But at least, it’s good to hear I can require these modules as local in main.lua, and once I do so, I don’t need to require them again in other module.

Thanks again! [import]uid: 67217 topic_id: 16401 reply_id: 61642[/import]