Comment by electroglyph
Comment by electroglyph 16 hours ago
it's easy, but the 1 indexes and global by default suck
Comment by electroglyph 16 hours ago
it's easy, but the 1 indexes and global by default suck
The 1 indexes are only a difference from what you're used to. Lua was made by mathematicians, who of course wanted to address the first element as 1, the second element as 2, etc.
0-indexing makes sense in the context of C where the index operator is syntactic sugar for pointer arithmetic. In higher-level languages like C# and Python and others, it's pretty much just a leftover habit from C devs that we all got used to.
Global by default is a perpetual issue, agreed.
> The 1 indexes are only a difference from what you're used to
The left handed scissors are only a difference from what you're used to.
> Lua was made by mathematicians
The default value is nil and using nil as an index on a table returns nil. Yet nil + number is not valid and results in a runtime error.
> it's pretty much just a leftover habit from C devs
It's reflective of the fact that these languages are either intended to work with C APIs or are implemented in C itself. This makes writing FFI and extensions _far_ easier than it would be otherwise.
You are correct that 1-based indexing is the norm among mathematicians, but none of the creators of Lua is a mathematician, AFAIK. You can read about the early history of Lua here:
Lua is the scripting language of [Recoil]. I've been writing some fairly complex game code on it for a few years now, I lost a lot of prejudice over time on 1-indexing.
In fact, at least for this application I've come to enjoy its practicality!
I wrote about this in:
https://andregarzia.com/2021/01/lua-a-misunderstood-language...
I like 1 based indexes and the globals don't hurt me on my projects. It is like JS, it has its own bits that are different than other languages, once you learn them you see that it is mostly fine.