[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Undefined variables returning nil
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 20 Mar 2013 18:43:35 +0200
2013/3/20 Marc Lepage <mlepage@antimeta.com>:
> My general opinion is that, if you are suffering from these kinds of errors,
> you are "developing too fast."
>
> Which is to say, if you took time to ensure there were no typos, added
> asserts, metatable index guards where appropriate, etc. then the problems
> would basically just go away. Yes it would take some effort. And the amount
> of effort and appropriateness of these measures depends on the program and
> the programmer. E.g. whether you tend to make a lot of typos, or how much
> syntax highlighting support your editor gives you.
>
> So in general, my advice is to ensure you are taking time and measures to
> deal with these issues, if you are having them. Just that's more of a
> development workflow issue than a language environment issue.
Cogently argued. The best precaution against typos is to avoid hasty,
shoddy programming habits.
> On Tue, Mar 19, 2013 at 4:23 PM, William Sumner <prestonsumner@me.com>
> wrote:
>>
>> Lua's behavior of silently returning nil for undefined variables can lead
>> to bugs that propagate undetected. Has thought been given to changing this
>> in a major version update, or are there reasons for retaining this behavior?
>> I'm aware of strict.lua, but it only provides safety for global/module
>> scope.
That description of Lua's behaviour is based on a false analogy with
languages like C that have actual variables that can be defined or
undefined.
There is no such thing as an undefined global variable in Lua. A Lua
variable is the association of a value with a name. That is to say, what
changes during an assignment statement is the association, not the value
or the name. By default, the value nil is associated with every name.
Literally every name, not only the ones for which the association has
explictly been stored.
The value nil has no memory. If you assign nil to a variable that
previously was associated with non-nil, that variable does not become
undefined. If you never assigned anything to a name before, that does
not mean the variable is undefined.
There is no such thing as an undefined local variable in Lua. A local
variable overrides the global association of a name. If no override has
been made, the name still refers to the global name.
Some Lua programmers understand this, some Lua programmers like this,
some Lua programmers love this. The key step is to understand this,
otherwise you will always feel uncomfortable with it.
Once you understand it, it becomes obvious that changing this would
result in a different language. It would no longer be Lua.