[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion: Deprecate "Attempt to index a nil value" error; instead, return nil or create table
- From: Sean Conner <sean@...>
- Date: Sun, 1 Mar 2020 18:21:57 -0500
It was thus said that the Great Anton Jordaan once stated:
>
> When writing: automatically assign a single-entry table to each
> undefined variable, with the given index the sole entry. For example,
> if t is an undefined variable, then t[a] = v would be syntactic sugar
> for t = {[a] = v}. Similarly, if t[a][b] is already declared as a table
> but t[a][b][c] is nil, then t[a][b][c][d][e] = v would mean t[a][b][c] =
> {[d] = {[e] = v}}.
Let's assume for a second this exists. Here are some of the issues that
need to be addressed for this to work.
1) t[a] = v
This is syntactic sugar for t = { [a] = v }. Then does that mean:
local t[a] = v
is legal? Otherwise, this may lead to a proliferation of unintended
globals. Also, what about:
t[a],t[b],t[c] = 1,2,3
How does this play out, syntactic surgar wise? Because as it is,
local t[a],t[b],t[c] = 1,2,3
is similar to:
local a,a = 1,2 print(a)
and it's the second declaration that survives (Lua 5.3). Weirder,
in the following:
a,a = 1,2 print(a)
it's the first declaration that survives. This may require more of
a change to Lua than expected.
2) t[a][b][c][d][e] = v -- when t[a][b] exists
An asusmption being made is that a,b,c,d,e are all tables. They
don't have to be. It could be a userdata that responds to both
__index and __newindex to act like a table. Then this is fine, but
what if b *doesn't* respond to __newindex? You either throw an
error, which is unexpected in this construct or replace b with a
table, which can cause extended debugging issues trying to figure
out why the code is failing (most likely in an area far from where
the bug actually exists!).
3) v = t[a][b][c][d][e]
v ends up nil. Which index is nil?
4) t[a] [ t[B] ] [c][d][e] = v
If t doesn't exist, then what do we end up with? If B doesn't
exist, what do we end up with? It's hard to think about. The
assumption is that a,b,c,d,e are all either strings or numbers, but
in reality, the index can be any value but nil or NaN. If tables
are created for missing values, then you end up with a unique table
as an index in a.
5) t[a][0/0][c][d][e] = v
The previous question brings this up---what if the index is NaN?
Rare, but possible. Treat it as nil? Error?
I know a reaction is "don't do that!" or "it's for the case when they are
all tables" but I contend that typos happen, bugs exist, people don't read
the manual closely enough and these issues *will* come up, and need to be
addressed (even if to detect and throw an error).
-spc