Starting at 1 is a convention:
array = {“hello”, “world”}
print(array[1]) —- displays “hello”
array[0] = “why”
array[2] = “there”
for i=0,2 do print(array[i]) end —- “why” “hello” “there”
Starting at 1 is a convention:
array = {“hello”, “world”}
print(array[1]) —- displays “hello”
array[0] = “why”
array[2] = “there”
for i=0,2 do print(array[i]) end —- “why” “hello” “there”
As shown by your example, arrays constructed the native way will start at 1. So you're going to need to make your own custom constructor which means additional overhead.
Also, all built-in functions that expects 1-based arrays will completely break:
for _, value in ipairs({[0]="foo", [1]="bar"}) do print(value) end
This will print "bar"And even if you still insist on pushing through all of this friction by handrolling your own stdlib, everything is out of the window once you interact with any external library that you don't control.
There's a very good reason why no one is using this trick. Anything other than 1-based indexing is just undefined behavior.
People absolutely do this, and you have to for things like using LuaJIT’s FFI and doing work across C.