lua-users home
lua-l archive

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


> Lua's 1-based native indexing 
> already annoys people

Errrr, which people exactly?

In C it is natural to have 0-based arrays because of the
meaning of an array index (low-level memory 
displacement from address at which first element is
stored, such that first element is zero bytes along in
RAM). So C 'indexes' aren't really indexes -- they are
just offsets which map directly onto the memory
semantics of C.

In Lua, 1-based arrays are natural. The first element is
at index 1, second at 2 and Nth at N. The index 
number is just the cardinal which, well, counts the 
item in the array. Who cares where it is in memory?
In fact, you can't even find out where it is, and you
do not need to.

After all, consider which is more natural-looking (pun
intended) as code to examine the first N items in a 
counted array:

  for (n = 0; n < N; n++) examine(n);

or:

  for n = 1,N do examine(n) end 

The former is just an unnatural idiom forced on C 
coders because that's how C works.

So people who are annoyed by Lua's 1-basedness are
merely being annoying themselves :-)