[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Questioning variable scoping in Lua
- From: David Kastrup <dak@...>
- Date: Tue, 20 Jan 2009 12:37:54 +0100
"Ben Aurel" <email@example.com> writes:
> Because I'm new to Lua I'm going through some cool Lua projects (lqt,
> nanoki,...) in order to see how the language is used in real world
> Even if you just look at the source from a distance, not necessarily
> trying to understand what the code actualy does, you notice the
> excessive use of the "local" keyword.
> - Setting the default scope to "global" why is that really usefull? I
> think most of the time, you try to avoid global state. Why not the
> default scope to "global"? Wouldn't that reduce a lot of typing?
> - What if the developer forgets a "local"? The program runs anyway,
> but there is a chance that there is some unwanted behavior that is
> hard to trace.
> I think I miss something here. Could somebody explain me the reason
> behind the design decision?
Lua is an interpreter, and if you use it in interpretive mode, each
chunk has its own scope:
> local y; y=3; y=y+1; print(y)
If the default was local, that would be inconvenient. In particular
since function definitions are assignments as well.