lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I see, when I change the variables from the libraries (or should I say modules, I really don’t know when to use what) I’ve written to local, they don’t carry over. So I guess that’s the only time you use globals then?
On Aug 7, 2014, at 3:05 PM, Steven Degutis <sbdegutis@gmail.com> wrote:

> This is not really specific to Lua, it's a general programming
> concept. Variables should be limited to the scope in which they're
> needed. Almost all variables you create are probably going to be
> locals. But every "built-in" function you're using is just a global
> variable. Only create globals when you need information to be shared
> between modules and if this is the most practical way of doing so
> (it's usually not but sometimes it is).
> 
> On Thu, Aug 7, 2014 at 12:18 PM, Mason Mackaman <masondeanm@aol.com> wrote:
>> So could I have an example of where one would want or need to declare a global variable?
>> On Aug 7, 2014, at 12:05 PM, Elias Barrionovo <elias.tandel@gmail.com> wrote:
>> 
>>> On Thu, Aug 7, 2014 at 2:02 PM, Coda Highland <chighland@gmail.com> wrote:
>>>> But you're exactly right -- you could do that, and many people DO
>>>> exactly that as an optimization.
>>> 
>>> More than that; it's generally considered a bad habbit to use globals,
>>> especially when creating libraries, or otherwise there's a good chance
>>> there will be name conflicts.
>>> 
>>> Now why lua variables are global by default is something that shows up
>>> quite often here on the mailing list. If you search the lua-l archives
>>> you'll se a lot of discussion about that. Key search terms would be
>>> "global by defaul", "local by default" and "lexical scoping".
>>> 
>>> --
>>> NI!
>>> 
>>> () - www.asciiribbon.org
>>> /\ - ascii ribbon campaign against html e-mail and proprietary attachments
>>> 
>> 
>> 
>