lua-users home
lua-l archive

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


On Thu, Mar 29, 2018 at 10:36 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
.....
> Negative indices are useful; take for example the 'arg' array. I much
> prefer the Lua way, where t[-1] means t[-1], to the Python way, where
> t[-1] means t[#t-1],

You are comparing lua tables, which are dictionaries with some fancy
optimization for positive integer indexes, to python lists, which are
not.

If you use the closer type to a lua table in python, a dict(), t[-1]
works as in lua ( and neary all other operations ).

> and to the C way, where t[-1] means "let's hope
> the pointer t is pointing to something that won't make the program
> crash".

t[-1] is used a lot in C, and means *(t-1), C is a totally different
beast than lua. C [] are a dereference operation, so you should be
"hoping" for it everytime you use it. I normally do not hope for my
pointers to be valid, I know they are. Sometimes I'm wrong, but this
is life. It's one of the problems of using a more low level language,
which lets you do more things, like implementing lua.

Francisco Olarte.