为什么 "return" 和 "break" 只能作为 Lua 代码块中的最后一条语句出现?
Why can "return" and "break" appear only as the last statement in a block of Lua code?
如 Lua 手册所述:http://www.lua.org/pil/4.4.html
我只对任何技术限制或约束感兴趣,对品味或编码标准相关的答案不感兴趣。
这对我来说似乎是一个奇怪的约束,因为悬挂 return
可以很容易地用语句 do return end
添加。
quote Lua 邮件列表中的一位作者:
For the return statement, it's because otherwise it may be ambiguous:
...
return
f(1,2,3)
...
Is this a return with no values or a tail call to f?
The restriction on the break statement is a leftover from an
experimental break-with-labels. "break f(x)" coud be "break f"
followed by "(x)".
对于 Lua 5.2 及更高版本,break
的限制已解除。
如 Lua 手册所述:http://www.lua.org/pil/4.4.html
我只对任何技术限制或约束感兴趣,对品味或编码标准相关的答案不感兴趣。
这对我来说似乎是一个奇怪的约束,因为悬挂 return
可以很容易地用语句 do return end
添加。
quote Lua 邮件列表中的一位作者:
For the return statement, it's because otherwise it may be ambiguous:
... return f(1,2,3) ...
Is this a return with no values or a tail call to f?
The restriction on the break statement is a leftover from an experimental break-with-labels. "break f(x)" coud be "break f" followed by "(x)".
对于 Lua 5.2 及更高版本,break
的限制已解除。