Scope: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==What is Scope?==
==What is Scope?==
Scope is a block of code in which variables are declared. <tt>for</tt> loops, <tt>while</tt> loops, <tt>repeat until</tt> loops, <tt>functions</tt>, <tt>if then</tt>, and <tt>do end</tt> chunks all create a new variable scope. For most of the following examples, the <tt>do end</tt> chunk will be used as a simple way of creating a variable.
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 variable.


Variables can be declared in two ways: local (with the <tt>local</tt> keyword), and global. Global variables are ''always'' in scope: no matter where you declare them, they are always accessible.
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|<code lua>
{{Example|<syntaxhighlight lang="lua">
function myFunction()
function myFunction()
myVariable = 64 --globally scoped
myVariable = 64 --globally scoped
Line 15: Line 15:
Output:
Output:
myVariable is 64
myVariable is 64
</code>}}
</syntaxhighlight>}}


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"
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". Take a look at this example using the {{`|local}} keyword to change the scope of a variable.


<code lua>
<syntaxhighlight lang="lua">
function myFunction()
function myFunction()
local myVariable = 64
local myVariable = 64
Line 27: Line 27:


print("myVariable is " .. myVariable) -->  myVariable is nil
print("myVariable is " .. myVariable) -->  myVariable is nil
</code>
</syntaxhighlight>


"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.
"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.
Line 33: Line 33:
Global variables can be set easily by removing the word local, but for even better control over a variables scope, you can use setfenv.
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|<code lua>
{{Example|<syntaxhighlight lang="lua">
function myFunction()
function myFunction()
function myFunction2()
function myFunction2()
Line 51: Line 51:
nil
nil
]]
]]
</code>}}
</syntaxhighlight>}}


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.
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.
Line 58: Line 58:
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.
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|<code lua>
{{Example|<syntaxhighlight lang="lua">
if true then --level 1
if true then --level 1
local a = 82.5
local a = 82.5
Line 65: Line 65:
end
end
end
end
</code>}}
</syntaxhighlight>}}


This is a simple example with two conditionals.  If you recall, conditionals also have their own environment.  Here is a snippet to prove this:
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|<code lua>
{{Example|<syntaxhighlight lang="lua">
if true then --level 1
if true then --level 1
local a = 82.5
local a = 82.5
Line 77: Line 77:
print(b) --> nil
print(b) --> nil
end
end
</code>}}
</syntaxhighlight>}}


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


{{Example|<code lua>
{{Example|<syntaxhighlight lang="lua">
if true then --level 1
if true then --level 1
local a = 82.5
local a = 82.5
Line 89: Line 89:
end
end
end
end
</code>}}
</syntaxhighlight>}}


Even though the second conditional has its own environment, because of lexical scoping, <tt>a</tt> was inherited.  Variables that are lexically scoped are called upvalues.  
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==
==See Also==

Latest revision as of 06:20, 27 April 2023

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 variable.

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". Take a look at this example using the local keyword to change the scope of a variable.

function myFunction()
	local myVariable = 64
end

myFunction()

print("myVariable is " .. myVariable) -->  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) --> nil
end


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) --> 82.5
		local b = 64
	end
end


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