为什么在 Lua 中使用 do-end 块? [英] Why use a do-end block in Lua?

查看:98
本文介绍了为什么在 Lua 中使用 do-end 块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找答案,但没有成功.我想知道,实际使用的do-end块是什么?它只是说在我的书中需要时使用值,那么我该如何使用呢?

I keep trying to find answers for this but fail to do so. I wanted to know, what is the do-end block actually used for? It just says values are used when needed in my book so how could I use this?

我是否通过将函数放置在do-end循环中并将局部变量放置在函数之外但在此do-end块内并将其用于函数的作用来减小局部变量的范围?但是然后仍然可以调用该函数吗?

Do I use it to reduce the scope of local variables by placing a function in a do-end loop and place local variables outside of the function but inside this do-end block and the variables will be seen by the function? But then can the function still be called?

很抱歉,含糊不清.我希望这是有道理的.也许一个说明性的例子可能有用^^

Sorry for being very vague. I hope that makes sense. Maybe an illustrated example might be useful ^^

推荐答案

do-end块与变量作用域的问题有关.本质上,当您使用标识符时,它具有什么值?例如,当我们编写以下程序时,将打印什么数字?

The do-end blocks have to do with the problem of variable scoping. Essentially, when you use an identifier, what value does it have? For example, what numbers will be printed when we write the following program?

local x = 10
if x > 0 then
    local x = 17
    print(x)
end
print(x)

当涉及局部变量时,Lua使用标准的词法作用域,如《 A》的第4.2节所述《 Lua编程》一书.词法作用域非常有用,原因有两个:

When it comes to local variables, Lua uses standard lexical scoping, as is well explained in section 4.2 of the Programming in Lua book. Lexical scope is very useful for a couple of reasons:

  • 变量作用域是静态的.您只需查看源代码,就能知道哪些变量和函数对应于代码中的每个标识符.这与您在Bash中发现的动态作用域或通过方法调用或数组查找进行的间接分配相反,在这种情况下,您需要考虑程序的执行流程,以了解最终将获得什么值.

  • The variable scoping is static. You know just by looking at the source code what variables and functions correspond to each identifier in your code. This is opposed to the dynamic scoping you find in Bash or indirect dispatching via method calls or array lookups, where you need to think about the execution flow of the program to know what value you will end up with.

可变范围界定是有限的,这有助于提高可读性并避免一些错误:

Variable scoping is limited, which helps readability and avoids some bugs:

  • 如果仅在需要使用变量时声明变量,则可以声明并同时对其进行初始化.另一方面,如果您在函数顶部声明所有变量,那么您可能会在初始化之前意外使用一个.

  • If you declare a variable only when you are going to need to use it you can declare it and initialize it at the same time. On the other hand, if you declare all your variables at the top of the function then you might end up accidentally using one before you initialize it.

如果在内部范围内定义变量,则不能在外部范围内意外使用它.

If you define a variable inside an inner scope you can't accidentally use it in outer scopes.

词法作用域可以将某些非常有表现力的习惯用法与嵌套函数(闭包)结合使用.

Lexical scoping enables some very expressive idioms when you combine it with nested functions (closures).

通常,您不必担心自己指定变量作用域.函数,循环和条件条件会自动引入新的作用域,通常这些作用域足以为您的变量提供受约束的作用域.就是说,您可能不时地想引入一些额外的示波器,我们可以使用do-end来实现.在Lua编程中,有一个下面的示例,您要在其中计算二次方程的解,并且该计算具有一些临时变量:

Usually, you don't need to worry about specifying variables scopes yourself. Functions, loops and conditionals automatically introduce new scopes and that will normally be enough for giving your variables a well constrained scope. That said, every once in a while, you might want to introduce some extra scopes out of thin air and we can use do-end for that. Programming Lua has the following example where you want to calculate the solutions of a quadratic equation and the computation has some temporaries:

do
  local a2 = 2*a
  local d = sqrt(b^2 - 4*a*c)
  x1 = (-b + d)/a2
  x2 = (-b - d)/a2
end          -- scope of `a2' and `d' ends here
print(x1, x2)

没有do-end块, a2 d 可能在不再需要后最终被意外使用:

Without the do-end block, a2 and d could end up being accidentally used after they are not needed anymore:

local a2 = 2*a
local d = sqrt(b^2 - 4*a*c)
x1 = (-b + d)/a2
x2 = (-b - d)/a2
print(x1, x2)

print(a2) -- OOPS! I wanted to say "print(a)"

也就是说,不需要经常使用do-end.如果代码块很小,则隐藏局部变量的需求就更少了;如果代码块很大,通常将其放在自己的子例程中是一种更好的方法.我发现do-end闪耀的时候是您只需要执行一次计算-函数可能会被调用很多次,但是如果您使用do-end块,您会清楚地知道您只在运行那段代码一次.

That said, do-end doesn't need to be used that often. If the code block is small, there is less need to hide the local variables and if the code block is big it often is a better approach to put the code block in a subroutine of its own. The times when I find that do-end shines is when you only need to do the computation once - functions can potentially be called many times but if you use a do-end block you make it clear that you are only running that piece of code once.

这篇关于为什么在 Lua 中使用 do-end 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆