Scope: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
Will finish later
>Camoy
lexical scoping section; formatted
Line 1: Line 1:
{{CatUp|Tutorials}}
==Scope==
__TOC__
===What is Scope?===
Scope is a block of code in which variables are declared. <code>for</code> loops, <code>while</code> loops, <code>repeat until</code> loops, <code>functions</code>, <code>if then</code>, and <code>do end</code> chunks all create a new variable scope. For most of the following examples, the <code>do end</code> chunk will be used as a simple way of creating a


== Introduction ==
Variables can be declared in two ways: local (with the <code>local</code> keyword), and global. Global variables are ''always'' in scope: no matter where you declare them, they are always accessible.


This tutorial will introduce you to <b>scopes</b> of [[Variables]].  It is recommended that you are familiar with the [[Variables]] and [[Functions]] tutorial.
{{Example|<pre>
function myFunction()
myVariable = 64 --globally scoped
end


== Discussion ==
myFunction()
<!--A scope is a block of code in which variables are declared. <code>for</code> loops, <code>while</code> loops, <code>repeat until</code> loops, <code>functions</code>, and <code>do end</code> chunks all create a new variable scope. For most of the following examples, the <code>do end</code> chunk will be used as a simple way of creating a
 
Variables can be declared in two ways: local (with the <code>local</code> keyword), and global. Global variables are ''always'' in scope: no matter where you declare them, they are always accessible
-->
A <b>scope</b> is what can access a variable.  Let's try, for example, printing the value of a variable that was declared in a function.  The following will give us an error:
 
<pre>
function myFunction()  
myVariable = 64
end


print("myVariable is " .. myVariable)
print("myVariable is " .. myVariable)
</pre>


The variable 'myVariable' was declared inside myFunction, and myVariable was created when myFunction was run.  Once myFunction ends, myVariable no longer exists.  That is why you can't "print" myVariable outside of this function.
Output:
myVariable is 64
</pre>}}


How do you print myVariable?  
When ''myFunction'' was called, myVariable was set to 64. Since this variable is globally scoped, it could be accessed outside the function.  This is why the output was "myVariable is 64"
 
You can declare outside of the function to have a "global scope."  


<pre>
<pre>
myVariable = 64 -- this value is declared outside of the function
function myFunction()
function myFunction()  
local myVariable = 64
end
end
myFunction()
print("myVariable is " .. myVariable)
print("myVariable is " .. myVariable)
Output:
myVariable is nil
</pre>
</pre>


Or you will have to call the function so that the variables within the function don't get destroyed:
"myVariable is nil" was outputted because myVariable was confined to myFunction's environment or its scope.  Therefore, it was not able to access the value outside of that function.


<pre>
Global variables can be set easily by removing the word local, but for even better control over a variables scope, you can use setfenv.
function myFunction() 
myVariable = 64
end
myFunction() -- the function is being called here
print("myVariable is " .. myVariable)
</pre>


Local variables can keep variables within the scope of the function or environment it is declared in.  Variable environments can also be set with getfenv and setfenv.
{{Example|<pre>
<pre>
function myFunction()
function myFunction()
local myVariable = 64
function myFunction2()
print(a)
end
myFunction2()
print(a)
end
end
setfenv(myFunction, {a = 64,print=print})
myFunction()
myFunction()
print("myVariable is ", myVariable) --> myVariable is nil
</pre>
This outputs nil because the word 'local' prevented the variable from being accessible outside of the function's environment even when the function was called.  Global variables can be set easily by removing the word local, but for even better control over a variables scope, you can use setfenv.
<pre>
function myFunction()
function myFunction2()
print(a)
print(a)
Output:
64
64
nil
</pre>}}
This will output two values of 64, and will output one nil.  The variable's environment was set to the function 'myFunction', and therefore the variable was also accessible by descendant environments (myFunction2).  The setfenv function was called taking in two arguments, the environment in which the variable was to be placed in, and a table with the variables to be set.  The variable 'a' was set, along with the reset for the global table.
===Lexical Scoping===
In the last example you may have noticed that I said "the variable was also accessible by descendant environments".  This concept is called lexical scoping.
{{Example|<pre>
if true then --level 1
local a = 82.5
if true then --level 2
local b = 64
end
end
</pre>}}
This is a simple example with two conditionals.  If you recall, conditionals also have their own environment.  Here is a snippet to prove this:
{{Example|<pre>
if true then --level 1
local a = 82.5
if true then --level 2
local b = 64
end
print(b)
end
end
myFunction2()
 
print(a)
Output:
nil
</pre>}}
 
Its nil because variable b was confined to the scope of the second conditional.  Lexical scoping will allow one to do this:
 
{{Example|<pre>
if true then --level 1
local a = 82.5
if true then --level 2
print(a)
local b = 64
end
end
end
setfenv(myFunction, {a = 64,print=print, _G = unpack(_G)})
myFunction()
print(a)
</pre>
This will output two values of 64, and will output one nil.  The variable's environment was set to the function 'myFunction', and therefore the variable was also accessible by descendant environments (myFunction2).  The setfenv function was called taking in two arguments, the environment in which the variable was to be placed in, and a table with the variables to be set.  The variable 'a' was set, along with the reset for the global table.


== See Also ==
Output:
82.5
</pre>}}
 
Even though the second conditional has its own environment, because of lexical scoping, a was inherited.  Variables that are lexically scoped are called upvalues.
 
===See Also===


[[Variables]]
[[Variables]]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Revision as of 13:38, 1 November 2010

Scope

What is Scope?

Scope is a block of code in which variables are declared. for loops, while loops, repeat until loops, functions, if then, and do end chunks all create a new variable scope. For most of the following examples, the do end chunk will be used as a simple way of creating a

Variables can be declared in two ways: local (with the local keyword), and global. Global variables are always in scope: no matter where you declare them, they are always accessible.

Example
function myFunction()
	myVariable = 64 --globally scoped
end

myFunction()

print("myVariable is " .. myVariable)

Output:
myVariable is 64


When myFunction was called, myVariable was set to 64. Since this variable is globally scoped, it could be accessed outside the function. This is why the output was "myVariable is 64"

function myFunction()
	local myVariable = 64
end

myFunction()

print("myVariable is " .. myVariable)

Output:
myVariable is nil

"myVariable is nil" was outputted because myVariable was confined to myFunction's environment or its scope. Therefore, it was not able to access the value outside of that function.

Global variables can be set easily by removing the word local, but for even better control over a variables scope, you can use setfenv.

Example
function myFunction()
	function myFunction2()
		print(a)
	end
	myFunction2()
	print(a)
end
setfenv(myFunction, {a = 64,print=print})
myFunction()
print(a)

Output:
64
64
nil


This will output two values of 64, and will output one nil. The variable's environment was set to the function 'myFunction', and therefore the variable was also accessible by descendant environments (myFunction2). The setfenv function was called taking in two arguments, the environment in which the variable was to be placed in, and a table with the variables to be set. The variable 'a' was set, along with the reset for the global table.

Lexical Scoping

In the last example you may have noticed that I said "the variable was also accessible by descendant environments". This concept is called lexical scoping.

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		local b = 64
	end
end


This is a simple example with two conditionals. If you recall, conditionals also have their own environment. Here is a snippet to prove this:

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		local b = 64
	end
	print(b)
end

Output:
nil


Its nil because variable b was confined to the scope of the second conditional. Lexical scoping will allow one to do this:

Example
if true then --level 1
	local a = 82.5
	if true then --level 2
		print(a)
		local b = 64
	end
end

Output:
82.5


Even though the second conditional has its own environment, because of lexical scoping, a was inherited. Variables that are lexically scoped are called upvalues.

See Also

Variables