Scope: Difference between revisions
>JulienDethurens |
>JulienDethurens Don't you dare use that tt tag! |
||
Line 1: | Line 1: | ||
==What is Scope?== | ==What is Scope?== | ||
Scope is a block of code in which variables are declared. | 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 | 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|<code lua> | ||
Line 91: | Line 91: | ||
</code>}} | </code>}} | ||
Even though the second conditional has its own environment, because of lexical scoping, | 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== |
Revision as of 02:40, 9 February 2012
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.
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) --> 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.
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.
This is a simple example with two conditionals. If you recall, conditionals also have their own environment. Here is a snippet to prove this:
Its nil because variable b was confined to the scope of the second conditional. Lexical scoping will allow one to do this:
Even though the second conditional has its own environment, because of lexical scoping, a was inherited. Variables that are lexically scoped are called upvalues.