← Back to context

Comment by int_19h

1 year ago

> Why are arrays tables

For the same reason why (standard) arrays are associative in PHP and JS, as well - because, from a certain perspective, a regular array is just an associated array with consecutive integers as keys, so if you're trying to keep things simple, it seems like a straightforward unification.

> why are they nil terminated

It follows from them being associated arrays, and Lua's pervasive use of "nil" meaning "this thing is missing".

> why do they start at 1

Because humans generally start counting from 1.

> Because humans generally start counting from 1.

I’m not parent, but for me the question is not literally why did they choose 1-based indexing. That’s easy to answer. The question is really: why haven’t they realized by now that the disadvantages of 1-based indexing far outweigh the benefits when it comes to programming, and changed it? Backward compatibility is a valid reason, but lots of other people have tried 1-based arrays and reverted to 0-based once it becomes clear that 1-based arrays causes more problems than it solves. Even humans who count starting from one will soon want to use 0-based the moment they need to add two array indexes together; we don’t want to have to start from 2 in that case, right? Early versions of the famous Numerical Recipes in C started with 1-based arrays, but removed them, and got slightly better. :P

  • Yes, that’s surely why Fortran never found any widespread adoption and remained an obscure, niche language.

    • Did you mean, "Julia never found any widespread adoption and remains an obscure, niche language"? Fortran was the world's top programming language until the new millennium and is still among the top ten most popular. Arrays in Fortran can start from any index; the default choice is 1, which can be overridden with literally one keystroke: 0.

      1 reply →

It is a misconception that a JS array is "just an associated [sic] array with consecutive integers as keys". Even the early (most readable) editions of the ECMA spec have a whole set of carve-outs that belies that claim.