21

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 behind it (like the scoping difficulties that I know cause the absence of a "continue" keyword in Lua).

AndersH
  • 808
  • 6
  • 7
  • Thanks lhf for the complete answer. And Lua uFAQ is new to me. Really good indeed! And through your link I discovered http://lua-users.org/wiki/DetectingUndefinedVariables. The tricks there solves the practical issue for me. – AndersH Oct 09 '10 at 09:38

1 Answers1

19

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.

H. Rittich
  • 814
  • 7
  • 15
lhf
  • 70,581
  • 9
  • 108
  • 149
  • 1
    Can you include relevant quote from the wiki? You know that answers on [so] should not be link-only. – user202729 May 27 '18 at 07:54
  • 4
    ["The problem is that without local declarations, you cannot say where the variable is local to." (RiciLake)](http://lua-users.org/lists/lua-l/2006-10/msg00063.html)[via the referenced Lua User's Wiki]—salient and succinct. – Tom Blodget Jul 05 '18 at 03:12