4 ms·
> Complaints about 1-based indexing for arrays ... > Using 1 as the initial position for an array makes sense when you’re talking like a human. If the target
by epage 4y ago
> Complaints about 1-based indexing for arrays
...
> Using 1 as the initial position for an array makes sense when you’re talking like a human.
If the target audience for the scrips is non-programmers, then sure.
The place where I found Lua's 1-based indexing unacceptable was when Lua was being introduced at a past job as a secondary language. The problem here is that many different programmers would be infrequently modifying the Lua code. In those cases, you need as few gotchas as possible to make sure people are successful and deviating from what everyone is used to in that environment for indexing does not lead to success.
- wsc981 4y agoSometimes I port some other language code to Lua and in such cases the 1-based indexing can be tricky. Though recently I discovered using a metatable to convert the index from 1-based to 0-based and vice versa can make the porting a bit smoother wrt this issue.
- kgeist 4y agoIndeed, since one of Lua's primary use cases is bindings to C/C++/etc., whenever you have C<=>interop you have to adjust indices.
- astrobe_ 4y ago> If the target audience for the scrips is non-programmers, then sure. It kind of is. Lua was developed as a configuration language "on steroids" for Petrobrass programs. The target users were non-programmers. Nowadays, Lua allows mildly tech-savvy persons to create or modify scripts. That includes high-school kids, who typically don't give a huck about Djikstra's arguments [1] ;-) The only way to avoid language "collisions" when you program in different languages in parallel is to choose languages vastly different in syntax. 1-based indexing is not "unacceptable", it is a drop in the ocean when you use together with C or C++. Typically you forget semicolons in C because Lua doesn't mandate it, or you forget that in Lua, zero is not "false" for if, etc. Personally I write some Forth on the side too, and as both Forth and Lua have the "then" keyword but used in a different way, I often misplace it. Blaming C, Lua or Forth for any of these difficulties doesn't make sense. The only answer, really, is "deal with it, it won't be the last time unless you quit programming". [1] https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- epage 4y agoI'm not blaming Lua. I'm saying that the context where it is used is important and can effect whether things are problems or not. Semi-colons are a bad example, you are generally not going to have subtle bugs from those. Off-by-one bugs are already bad enough in programming, having people switch their base index between tasks will not help.
- astrobe_ 4y agoI agree that semantic mismatches are a worse problem than syntactic mismatches. However in the case of Lua, because of dynamic typing and late binding, it would not be wise to not have unit tests and full test coverage. This mostly reduces semantic mistakes (indexing, 0-is-not-false, etc.) mostly to annoyances like syntax errors. "mostly" because some "stateful" operations need additional test efforts, and finding out why a test fails is not always straightforward.