lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Tue, Mar 13, 2018, 2:44 PM Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> btw, I notice that there is a new keyword, "undef", not mentions in manual,
> is that just a undocumented feature or the doc just not the latest?

I am grad you asked :-)

Lua 5.4 has an experimental change that is off by default (to keep
compatibility). We plan to keep this off in Lua 5.4 final.
You can turn it on by compiling this version with the option
-DLUA_NILINTABLE. As the name hints, this option allows nils in tables:

>  t = {nil, nil, nil}
> #t
3
> for k,v in pairs(t) do print(k, v) end
1       nil
2       nil
3       nil
> t[#t + 1] = nil
> for k,v in pairs(t) do print(k, v) end
1       nil
2       nil
3       nil
4       nil


Once you have nils in tables, t[i]=nil does not remove elements from
a table anymore. For that, you need to write t[i]=undef.

I'm glad this is experimental!

You could turn this around, no? Don't change the meaning of nil assignment; just extend the language (instead of changing it) by adding undef assignment. Or better, nil! assignment (as in, "I rilly mean it!):

t[I]=nil -- not really, just del the entry

t[I] = nil! --no, really!

You don't need NILINTABLES.
...

t[i]=undef is a special syntax form that means "remove the key 'i'
from table 't'.

Don't we already have that?

You
can only use 'undef' in three specific forms:

Which means undef is more than a new keyword.

As long as you are introducing new syntax, why not a new operator, e.g.

t[I]-!  --remove i from index of t

Or

t[I] =! nil --really assign nil to entry!

Or similar. 

It's a fun problem.

Gregg