lua-users home
lua-l archive

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

Cosmin Apreutesei <> wrote:

> Just found out that the __index metamethod is called for x=a[nil], but
> __newindex is not called for a[nil]=x -- instead the operation is
> specifically forbidden. I'd rather have it called too and not throw an
> error. What am I missing?

I suspect, but have not verified, that (1) any value received by
__newindex as its first argument (index) is a value which will not
cause rawset to throw an error when passed to rawset as its second
argument (index); and (2) any value received by __index is a value
which will not cause rawget to throw an error when passed to rawget as
its second argument (index).

But that observation does not address your question, which has two parts:
What should a[nil]=x do?  and  What should x=a[nil] do?

The first question is easy, since Lua forbids nil as a table index.
Semantically, a[k] on the left side of an assignment statement must
denote a location in the "store" (storage, i.e. memory).  Since a[nil]
does not denote a location, the assignment cannot be accomplished.  An
error must be thrown.

The second question verges on the meta-physical.  In an expression,
a[k] denotes the value in the store at a particular location.  But
a[nil] is not a valid location, so it makes no sense to ask "What is
the value stored in this non-existent location, a[nil]?"

When a question makes no sense, does it matter what the answer is?
(What is the sound of one hand clapping?)  A language designer can
rule it illegal to even ask the question, e.g. Scheme forbids asking
(car '()).  Or, an arbitrary answer can be defined because it is
convenient, e.g. in Lisp (car nil) is nil.

In Lua, the non-sensical expression a[nil] is simply defined to be
nil, for any table a.

(Compare to how 0! can be, and is, defined to be 1 for convenience,
when in fact it makes no sense to ask "what is the value of 0!" when
factorial is defined as n!=(n)(n-1)...1.)

If we were to write a formal semantics for Lua, we could describe the
table index operator [] as a partial function from the domain of all
Lua values to locations in the store when a[k] is the left side of an
assignment (partial because nil and NaN do not map to locations).  And
we could describe the same operator [] as a total function from Lua
values to Lua values when a[k] appears in an expression.

This corresponds to an intuitive definition such as this one: "The
expression a[k] returns what is in table a at index k.  Since nil is
an illegal index, there can be no value stored at a[nil], and 'no
value' in Lua is represented by nil.  So, a[nil]==nil."

If you have read this far, you are a patient person.  This is my first
post to the Lua mailing list, provoked by Cosmin's interesting
observation.  I have possibly violated the norm here, which seems to be
short posts.  Apologies to all if this is the case.

--Jim Jennings