Scope modifiers: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: add missing identifier) |
(→{{header|Lua}}: added Lua solution) |
||
Line 713: | Line 713: | ||
extends(public::minimal)). |
extends(public::minimal)). |
||
</lang> |
</lang> |
||
=={{header|Lua}}== |
|||
In Lua, variables are global by default, but can be modified to be local to the block in which they are declared. |
|||
<lang lua>foo = "global" -- global scope |
|||
print(foo) |
|||
local foo = "local module" -- local to the current block (which is the module) |
|||
print(foo) -- local obscures the global |
|||
print(_G.foo) -- but global still exists |
|||
do -- create a new block |
|||
print(foo) -- outer module-level scope still visible |
|||
local foo = "local block" -- local to the current block (which is this "do") |
|||
print(foo) -- obscures outer module-level local |
|||
for foo = 1,2 do -- create another more-inner scope |
|||
print("local for "..foo) -- obscures prior block-level local |
|||
end -- and close the scope |
|||
print(foo) -- prior block-level local still exists |
|||
end -- close the block (and thus its scope) |
|||
print(foo) -- module-level local still exists |
|||
print(_G.foo) -- global still exists</lang> |
|||
{{out}} |
|||
<pre>global |
|||
local module |
|||
global |
|||
local module |
|||
local block |
|||
local for 1 |
|||
local for 2 |
|||
local block |
|||
local module |
|||
global</pre> |
|||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |