lua-users home
lua-l archive

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


If I thinking about starting and ending indicies is
actually relevant, the fault is in the programming
language.

In some senses, Lua is much more flexible in this regard than many programming languages, because you genuinely don't have to think about the indices at all. (In C, for example, not only do you have to remember to start at 0 and to end most strictly at size-1, you also have to remember to use numeric indices only. In Lua, your indices can be any Lua value, with the notable exception of nil. So you can index tables with tables, functions, integers, strings.)

But with this freedom comes responsibility -- how to interpret the indices in your table is entirely up to you. There is no such thing as a "vector."

There is a single, special kind of table, viz: the array, which depends upon being indexed from 1 to n, with no gaps in between. Some library functions, plus the '#' operator, exist which in turn depend upon this internal layout. Don't like? Don't use.

Incidentally, this isn't as crazy as it sounds. Lua could be fitted with several special sorts of table, to suit everybody, at the expense of size and speed. It issn't -- the so-called "array" is the only special type of table and the slightly "non-deterministic" behaviour of # is like it is for reasons of efficiency. (You can find the largest integer index used in a table with the table.maxn() function, by the way. But it's not as quick as using #.)

Given the weirdness of "vectors", what am I likely to find with
"strings"?

Depends what you think a "string" is supposed to be. If you are a C programmer, you'll be amazed how reliable and easy they are. If you are a FORTRAN programmer, you probably won't need them :-)

Oh, strings are immutable (like Python), which might annoy you if you believe that this is a bad semantic choice. So every time you modify a string, for example by chopping a character off the end, you get a new string. Again, this is a feature, unless you expect Lua to be exactly like <some other language which has mutable strings>.

I don't want to have to *care* about a vector index start *or* end.
It should be completely opaque.
[snip]
Either the semantics need to be cleaned up, or it really needs
to be split out into a separate syntactic structure/system/etc.

Someone has to care, in order to deliver the programmatic facility you want which permits you not to care. (RAM doesn't organise itself.) In Lua, as someone (PA?) said the other day, with Lua you often have to bring your own batteries...again (look at the compiled size of the Lua binaries :-) this is both a strength and a weakness. In this sense, "vectors" are already split out into a separate system.