在诸如 C 之类的编程语言中,您可以创建一个匿名代码块来将变量的范围限制在 block 内,Lua 也可以这样做吗?
如果是这样,以下 C 代码的 Lua 等效项是什么?
void function()
{
{
int i = 0;
i = i + 1;
}
{
int i = 10;
i = i + 1;
}
}
最佳答案
您想使用 do...end
.来自 manual :
A block can be explicitly delimited to produce a single statement:
stat ::= do block end
Explicit blocks are useful to control the scope of variable declarations. Explicit blocks are also sometimes used to add a
return
orbreak
statement in the middle of another block
function fn()
do
local i = 0
i = i + 1
end
do
local i = 10
i = i + 1
end
end
关于lua - 你能在 Lua 中创建匿名代码块吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712599/