开发者

Why does Lua default to global variables?

开发者 https://www.devze.com 2023-01-19 05:53 出处:网络
My favourite language these days is Lua. I have only one issue with it, why on earth is its default behaviour that variables in functions are global? In the similar language Icon there is a keyword \"

My favourite language these days is Lua. I have only one issue with it, why on earth is its default behaviour that variables in functions are global? In the similar language Icon there is a keyword "global" that is used when one really wants to use a global instead of the natural behaviour to default to local (I was bitten by this again five minutes ago). I would feel better about this if somebody could tell me the rational behi开发者_C百科nd it (like the scoping difficulties that I know cause the absence of a "continue" keyword in Lua).


See Why aren't variables locally scoped by default? in the Lua uFAQ.

It certainly feels easy to only explicitly declare globals when they are in a local context. The short answer is that Lua is not Python, but there are actually good reasons why lexically-scoped local variables have to be explicitly declared. See the wiki page.

0

精彩评论

暂无评论...
验证码 换一张
取 消