[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Undefined variables returning nil
- From: steve donovan <steve.j.donovan@...>
- Date: Wed, 20 Mar 2013 09:53:18 +0200
On Wed, Mar 20, 2013 at 9:06 AM, Sean Conner <sean@conman.org> wrote:
> -spc (Half the time, I want to declare all my variables and table fields,
> the other half, I don't ... )
That's the rub ;).
Strongly-typed tables are a very useful technique for non-trivial programs:
http://lua-users.org/wiki/StrictStructs
http://steved-imaginaryreal.blogspot.com/2011/09/strict-tables-in-lua.html
(the last article is relevant to the nil-access-should-be-an-error debate)
Basically, we cheerfully put all sorts of things into plain tables;
with a _little_ more typing (both senses meant) we give most tables an
_identity_ and can then do dynamic spelling-mistake detection on them.
It also helps with the old problem of how to document arbitrary table
arguments to functions.
I understand William's anxiety, but the language provides the
meta-mechanisms to be as strict as you need to be. It's a pity that
there isn't a 'standard' module that does the job (but 'standard
libraries' in Lua is a nebulous concept [1])
An interesting speculation: what if Lua was shipped with a few little
Lua modules and they acquired 'canonical' status. E.g., a standard way
to generate 'classes'[2] and a standard way to be strict. How would
that have affected the development of the Lua ecosystem?
Also, having a static analysis tool like lglob around is very helpful
as a compile check. (Yes, I _know_ it doesn't do the _ENV 5.2
pattern, but personally I think _ENV is just a more elegant kind of
magic than module() and setfenv(), and magic always has its cost.)
steve d.
[1] in the modern sense: "the Cloud will Provide..."
[2] yes, I know there's no such thing. But there's a common metatable
pattern that suits most uses.