gpt4 book ai didi

lua - 你能在 Lua 中创建匿名代码块吗?

转载 作者:行者123 更新时间:2023-12-04 16:51:42 24 4
gpt4 key购买 nike

在诸如 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 or break 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com