lua-users home
lua-l archive

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


I'm just trying out Lua. I'm liking a lot of things; however, the use of tables as vectors is just perplexing:

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> t = { 1,1,2 }
> = # t
3
> t1 = {}
> = # t1
0
> t1[0] = 12
> = # t1
0

The Lua manual states:
"The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil"

But even *that* clearly isn't the case since # for an empty vector is 0 (which is nil). So, even the Lua definition of # violates its preconditions.

Yes, I'm a "0 is the only real starting index" bigot. This bugs me a little, but not hugely. I figure I'll get over it.

Here's what really bugs me:
If I thinking about starting and ending indicies is actually relevant, the fault is in the programming language.

I don't want to have to *care* about a vector index start *or* end. It should be completely opaque. The failure is that tables as vectors have whacky behavior:

> t = {1, 2, 3}
> for i,v in ipairs(t) do print(i, v) end
1       1
2       2
3       3
> t[4] = 4
> for i,v in ipairs(t) do print(i, v) end
1       1
2       2
3       3
4       4
> t[6] = 6
> for i,v in ipairs(t) do print(i, v) end
1       1
2       2
3       3
4       4
> t[5] = 5
> for i,v in ipairs(t) do print(i, v) end
1       1
2       2
3       3
4       4
5       5
6       6
> t3 = {[2]=2, [3]=3}
> = # t3
0

Yeah, I understand the model in terms of the underlying implementation. However, I shouldn't have to think about this.

This is not behaving as a vector in the sense that anybody coming from *any* other language is used to. The fact that the extension is "nil" for a numeric type is going to be a surprise. The fact that if I assign to [0], ipairs will ignore it is a suprise. The fact that ipairs() stops at the first nil will be a surprise. The fact that ipairs() can pick up multiple extra entries from the assignment of a single entry is a surprise.

In short, using "vectors" in Lua is a minefield of weirdness for new folks. Either the semantics need to be cleaned up, or it really needs to be split out into a separate syntactic structure/system/etc.

While I like quite a bit of Lua, this is enough to make me very concerned about what other things are lurking in the language. Given the weirdness of "vectors", what am I likely to find with "strings"?

I don't like that feeling.

-a