This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Lord_Spelunky's profile picture

Locals? What Use Are They?

Started by Lord_Spelunky, 29 July 2013 - 09:41 AM
Lord_Spelunky #1
Posted 29 July 2013 - 11:41 AM
I might sound like a massive idiot here, so please be kind ;)/> as you usually are.

I have noticed that people say they really should use locals, and I don't know why? I can't even make assumptions. Could anyone enlighten me?
Cozzimoto #2
Posted 29 July 2013 - 12:07 PM
in a program when you classify a variable like so

local someVar = "name"
that someVar variable is deleted from the computer memory when that program exists

but when you do something like this

someVar = "name"
you just classified that someVar as a global variable and will not be deleted from memory unless set to nil or the computer is rebooted, this is very important if you do not want to overflow that stack and have the computer crash

the only thing i really use global variables for is peripherals on my computer, so my api and all of my programs on one computer can acces that same variable without assigning it to something before hand.
Lord_Spelunky #3
Posted 29 July 2013 - 12:32 PM
in a program when you classify a variable like so

local someVar = "name"
that someVar variable is deleted from the computer memory when that program exists

but when you do something like this

someVar = "name"
you just classified that someVar as a global variable and will not be deleted from memory unless set to nil or the computer is rebooted, this is very important if you do not want to overflow that stack and have the computer crash

the only thing i really use global variables for is peripherals on my computer, so my api and all of my programs on one computer can acces that same variable without assigning it to something before hand.

Thanks you, I have been enlightened :)/> Well explained as well
apemanzilla #4
Posted 29 July 2013 - 01:26 PM
Theres a bit more to it.
If you define a local var within a structure like a loop, function, or if, it is deleted after that structure ends. For example:

if 1==1 then
local a = "sometext"
end
print(a)
This would print nil, as a has been deleted and is now undefined.

It gets even weirder. Say you have a program with an if statement and a local variable of the program, not the if. If you access the variable from the if, it works as expected and returns whatever the variable is. But if you have a local var inside the if with the same name… Here, I'll just show you an example:

local a = "sometext"
if 1==1 then
  local a = "some different text"
  print(a) --Will show "some different text"
end
print(a) --Will show "sometext"
Lord_Spelunky #5
Posted 29 July 2013 - 03:00 PM
Theres a bit more to it.
If you define a local var within a structure like a loop, function, or if, it is deleted after that structure ends. For example:

if 1==1 then
local a = "sometext"
end
print(a)
This would print nil, as a has been deleted and is now undefined.

It gets even weirder. Say you have a program with an if statement and a local variable of the program, not the if. If you access the variable from the if, it works as expected and returns whatever the variable is. But if you have a local var inside the if with the same name… Here, I'll just show you an example:

local a = "sometext"
if 1==1 then
  local a = "some different text"
  print(a) --Will show "some different text"
end
print(a) --Will show "sometext"

Ahhhhh, so they become like 1 time variables, thank you :)/> you helped a lot. +1 for both of you!
immibis #6
Posted 29 July 2013 - 05:08 PM
The reason to use locals is so that other programs, and other parts of your program, can't accidentally overwrite them.
albrat #7
Posted 29 July 2013 - 06:57 PM
I would like to say thank you to you all as well, I learnt something new as well. (I previously had not known this).
Qix #8
Posted 30 July 2013 - 06:18 AM
For the more hardcore Lua users who may understand this: Globals are stored in the table _G, whereas locals are stored as up-values. This is the same for scripts and other scoped "blocks" (closures, since scripts are simply closures).

Up-values are simply values held on the Lua stack and are given 'pseudo-indexes', or indexes that don't refer to an absolute index, but a relative one. This facilitates their removal from memory when the closure loses scope, instead of being stored in another table.

This usually doesn't matter when you're working within the Lua language, but it does when working with Java or, more commonly, the C API.
theoriginalbit #9
Posted 30 July 2013 - 06:24 AM
I would also like to point out that the local variables are not always deleted immediately after their scope has finished.

With LuaJ the Java garbagecollector will go around and destroy any variables that are not referenced by anything. The garbage collector will run whenever it is needed by the OS (or invoked by the programmer) and is not permanently running.

A definite advantage to localising variables is having functions and/or variables in APIs that no one can interact with, think of them as hidden internals.

Lastly if you like to read official documentation, you might want to read this
Qix #10
Posted 30 July 2013 - 06:39 AM
I would also like to point out that the local variables are not always deleted immediately after their scope has finished.

With LuaJ the Java garbagecollector will go around and destroy any variables that are not referenced by anything. The garbage collector will run whenever it is needed by the OS (or invoked by the programmer) and is not permanently running.

A definite advantage to localising variables is having functions and/or variables in APIs that no one can interact with, think of them as hidden internals.

Lastly if you like to read official documentation, you might want to read this

Correct - since the objects are allocated as Java objects, they're subject to Java's garbage collector (since LuaJ is implemented 100% in Java). In C, they are deleted as soon as their reference count hits zero, unlike Java.

Also, good to know CC uses LuaJ :)/> I wasn't sure which library it used.
Kilobyte #11
Posted 30 July 2013 - 05:17 PM
Ahhhhh, so they become like 1 time variables, thank you :)/> you helped a lot. +1 for both of you!
Sounds misunderstandable so let me clarify it.
They are only visible to the 'block' and its 'subblocks'. they can be written or read as often as you want. a nested function for example can access upvalues of its parent.

Example:

local function makeHello(who)
    return function()
	    print "Hello "..who
    end
end
-- call it
local h = makeHello("World")
h() -- prints 'Hello World'
as you see, functions can also be declared as local