lua-users home
lua-l archive

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


Lua source code has a constant lua_Number set to double as default. In the
same way, it might be interesting to define a constant lua_FirstIndex (is
only an example of name) set to 1 as default, to refer to the first index
and array is indexed. Every part of Lua related with the first index of an
array (for instance, ipair, foreachi, etc.) should use this constant. In
this way, if a user (this is my case) wants to change this constant to 0
and recompile, he/she could use Lua tables beginning from 0 at his/her own
risk of loosing compatibility.

The same constant should be accessible from Lua to allow also scripts
being independent of the first value of index arrays or at least to detect 
whether or not they can run properly.

Even for people who believe that this value should not be changed, I think
they would admit that using this constant clarifies the code. For
instance, in the following piece of code (file src/lib/lbaselib.c, line
238):

    lua_pushliteral(L, "ipairs");
    lua_rawget(L, LUA_GLOBALSINDEX);  /* return generator, */
    lua_pushvalue(L, 1);  /* state, */
    lua_pushnumber(L, 0);  /* and initial value */
    return 3;

I think it would be better to write:

    lua_pushnumber(L, lua_FirstIndex-1);  /* and initial value */

instead of

    lua_pushnumber(L, 0);  /* and initial value */

I would like to help (but I am new in Lua and I do not master the source
code). Anyway, I hope someone take this into consideration. Thank you for
your time and best regards,

Salvador Espana