lua-users home
lua-l archive

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


* Klaus Ripke:

> Could x[] be syntactic sugar for x[#x+1] ?
> It would make appending to lists so much nicer.

"x[] = 5" looks about as cryptic to newcomers as "x[#x+1] = 5".  I'm
not sure if it is an improvement.  If this operation is so common that
it warrants its own syntax, something completely different might be
better.  But then, "table.append(x, 5)" doesn't look too bad, either.

> Could indexing anything but a table, or at least indexing nil,
> return nil instead of throwing an error?

I think there will be disagreement about that.  IMHO, Lua rather
throws too few exceptions than too many, hiding the original causes of
bugs.  For Java, the Elvis operator "?:" and "?." have been proposed:
A ?: B expands to (A == null ? B : A), while A?.B expands to
(A == null ? null : A.B), except that A is evaluated only once.
I don't really like them, though.  Using null/nil as an expected
value is often the wrong approach.