lua-users home
lua-l archive

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


"Wesley Smith" <wesley.hoke@gmail.com> writes:
> The other strong argument IMHO is when you're biding C/C++ libraries
> that use data structures like vectors and other types of 0-based
> iterators which when bound as userdata to Lua become confusing because
> a coice has to be made...conform to C and use 0-based arguments to its
> functions or conform to Lua and use 1-based.

Indeed... I have a friend that works on a commercial game engine that
uses Lua.  The "mismatch" between Lua array conventions and the
convention used by the underlying C++ engine is one his number-one
complaints about Lua.

[This is made worse because different parts of their software were
written by different people, who sometimes chose _different_ solutions
to the problem -- some chose to use Lua conventions in the Lua
interface, and "adjust" parameters passed to C, and others chose to
expose the C conventions to Lua, and the result is of course a huge
mess.]

I've heard many people who like 1-based offsets say something like
"Oh that's just a C/C++ thing, Lua is a different language, and Lua
doesn't deal with arrays like C".  However, I don't think you can just
ignore the issue like that -- it's a fact of life (for better or for
worse) that C/C++ (and others influenced by them, like Java) and 0-based
arrays are an utterly _huge_ presence in modern software, and this is
particularly true for Lua which is often used as a layer over a
non-trivial underlying system written (you guessed it) C or C++.

My personal take is that I _like_ 0-based arrays better -- they simply
make more sense to me -- but I have no problems with with an isolated
1-based system.  However I almost never used Lua standalone, and 1-based
arrays are a much bigger pain when dealing with interfaces to a C
program, mostly because it requires a mental switch when working on
different parts of the system.

Anyway, I'm not arguing that Lua should _change_ it's behavior --
there's way too much existing code for that -- but rather that it would
be nice if Lua could _support_ 0-based "array tables".

One way that comes to mind is that tables should simply have a
"min_index" field to store the minimum index for array-tables, instead
of always using 1.  Then "#table" could be implemented (internally) as
(table.max_index - table.min_index + 1), and all array indexing would
internally be implemented as (table.array[index - table.min_index]).

As for how to _set_ min_index, I guess something like:  when adding the
first indexed element, if it has an explicit (integer) key, use that to
set min_index, otherwise use 1.

So { "a", "b", "c" } would have min_index == 1
but { [0] = "a", "b", "c" } would have min_index == 0

Then perhaps there could be a syntax for creating empty tables with a
min_index, something like {[0]} (which is is currently a syntax error).

-Miles

-- 
It wasn't the Exxon Valdez captain's driving that caused the Alaskan oil spill.
It was yours.  [Greenpeace advertisement, New York Times, 25 February 1990]