4 ms·
I'm not OP, but I couldn't get past the fact that arrays start at 1 rather than 0.
by suby 9y ago
I'm not OP, but I couldn't get past the fact that arrays start at 1 rather than 0.
- seibelj 9y agoTotally agree with you. Zero-indexed arrays are one of those language design dogmas that are simply set in stone, really awkward to switch between languages when Lua has such a fundamental difference of opinion.
- grzm 9y agoWhile many languages follow a 0-indexed convention, as you see here, not all do. Another couple of cases I run into are Postgres arrays, and switching between “first”, “second”, and “nth” in Clojure. If it helps, you can think 0-indexed as referring to the offset, and 1-indexed as referring to the position. It’s a shame to dismiss the entire language due to what one might argue is a minor aspect.
- runevault 9y agoClojure being a lisp also has different enough syntax it is easier to jump back and forth because you're doing way more context switching. Lua looks enough like other c-style languages you can forget it is 1 based and make all sorts of nasty mistakes. You can certainly argue it is unfair, but it is still something that increases the risk of mistakes.
- setzer22 9y agoReplacing (first coll) by (nth 1 coll) in clojure happens to me more often than I'd like to admit
- synthc 9y agoI would not call Clojure 1-indexed though because of 'first', it uses 0-indexing for list, vectors, and arrays.
- grzm 9y agoTo be clear, I’m not calling Clojure 1-indexed: I provided “nth” and “first” as examples of contrasting 0- and 1-based conventions; in this case within the same language.
- anta40 9y agoI mostly write in Java for work. Back to the good old high school days, I spent 2 years learning programming in Pascal (yes, Turbo Pascal for DOS!), so seeing arrays started at 1 is not something strange for me :) Of course, in Pascal you actually can pick any number where the index starts/end.
- rogual 9y agoFor me it was the fact that the array ends at the first 'nil' you store in it. 1-based arrays I could get used to; that was just maddening.
- accatyyc 9y agoSince Lua “arrays” are just tables with numerical keys, couldn’t you just start at 0 if that’s your preference?
- snarfy 9y agoThey don't have to, since Lua tables are hash sets and you can use anything as a key. You can start them at 0 if you want. table[0] = "foo" is valid and works. The downside is you have to add '-1' in a few places, but it's not as many as you'd think. All of my Lua code uses zero based arrays. I think the only place I had to add -1 was initializing numeric for loops.