Ask HN: Can Lua be remade to use 0-based numbering?

Lua seems like such a fun, simple, efficient language... but even on HN, people hate on it almost to the point of an absurd meme simply because its 1-based indexes.

If you search the forums, this fundamental issue is mentioned thousands of times over years and years.

Which leads me to a naive question: Lua is open sourced. Why hasn't anyone in the world just forked the language to be 0-based? Is it not as simple as it sounds? I don't know what goes into designing and compiling a programming language, so it seems like such a trivial fix for what appears to be the #1 issue for every person out there allergic to Lua.

14 points | by Solvency 315 days ago

12 comments

  • jstx1 315 days ago
    > appears to be the #1 issue for every person out there allergic to Lua

    It isn't.

    Having worked with languages that have both 0- and 1-based indexing (sometimes at the same time), this is at the bottom of my concerns when evaluating a language. It's the "tabs vs spaces" of programming language comparison. Right there with "does it have semicolons" and "is whitespace significant". None of these things are important.

    • Solvency 315 days ago
      Are you allergic to Lua? If so, then what is the primary reason? If you're not allergic to Lua, then you're not who I'm referring to. Because if you search HN, the 1-base issue comes up disproportionately higher than any other sentiment, good or bad.
      • giraffe_lady 314 days ago
        I'm not the person you asked but. I've used lua professionally, it's one of my least favorite languages to work in, and I don't give a shit at all about 0 vs 1 index.
      • lcnPylGDnU4H9OF 314 days ago
        My understanding is that the difference between 0-indexing and 1-indexing manifests in algorithms which iterate over a list. If the start of a list is at index 0, the end is at index "length - 1" and if the start of a list is at index 1, the end is at index "length". Neither fundamentally changes such algorithms.

        That's why it's so low on the list of importance when considering language features; it doesn't fundamentally change what the language does or how it's organized (perhaps beyond some contrived examples) and instead only affects what the code looks like.

      • jurimasa 315 days ago
        If they can't overcome such a simple thing, it's just not for them.
  • logicalmonster 315 days ago
    Outside of "programming muscle memory" (for lack of a better phrase) maybe interfering with your thought process when you're working through some random algorithms, what about 1-based indexing causes problems for people?

    I've never gone too far deep into 1-based languages, but it seems like it would avoid a lot of off-by-one errors once you get used to it.

    • bobbylarrybobby 314 days ago
      When hand rolling a multi dimensional array, div/mod are really pleasant when using 0-indexing and are incredibly painful when using 1-indexing.
  • jbluepolarbear 315 days ago
    You can change the offset in the Lua C source. It’s gonna break every single table you reference, but you can use your 0 based indexing in your scripts. Why though? I’ve used Lua for over 10 years and never had an issue with the 1 based index (I mostly use 0 based in c like languages.) I’ve never worked with anyone that cared about the 1 based indexing beyond their initial introduction to the language.
    • arp242 315 days ago
      I make off-by-one errors embarrassingly often, no matter if it's 0 or 1 based. Maybe I should try 2-based indexing.
  • rleigh 314 days ago
    So in Lua the "first" element has an index of 1. There's absolutely nothing wrong with this. And most people who haven't already been exposed to C and related languages would find this perfectly natural. Because it's an index number, not an offset.

    It's only in C and C++ where the memory location of an element can be expressed as base+offset that we end up using the offset rather than an index. That has clear logic behind it and absent optimising compilers it may be marginally more efficient, but it does not make for the most user-friendly interface--it does break the expectations of most other people.

    If I'm working on providing a Lua library interface for non-developers, they will find the default Lua behaviour far more natural than were I to force zero-based indexing upon them.

    • Someone 314 days ago
      > So in Lua the "first" element has an index of 1. There's absolutely nothing wrong with this.

      I’m beginning to wonder whether a language that doesn’t have indexes at all would be viable.

      Swift abandoned C-style for loops (https://github.com/apple/swift-evolution/blob/main/proposals...)

      It still has enumerated (https://developer.apple.com/documentation/swift/sequence/enu...()), though.

      That’s a special case of zip, though, and programs could use

        zip(items, 0...Int.max)
      
      or

        zip(items, 1...Int.max)
      
      according to their preference.

      Also, slices could be created using

        items.subrange( skip: 3, take: 5)
      
      instead of

        items.subrange( startIndex: 3, numItems: 5)
      
      Implementation-wise, those are identical, but conceptually, the first doesn’t use indexes
  • constantcrying 315 days ago
    Of course this is "fixable", Lua is just software.

    There is also nothing wrong with One based indexing. Zero based indexing exists for historical reasons and because most people are comfortable with it they feel uncomfortable with the alternative, that is easily overcome though.

    As an aside, the One based indexing is the mathematical convention, which is why e.g. MATLAB consciously choose it.

    >Why hasn't anyone in the world just forked the language to be 0-based?

    Significant amount of effort for an extremely minor change.

  • oweiler 314 days ago
    > Lua seems like such a fun, simple, efficient language... but even on HN, people hate on it almost to the point of an absurd meme simply because its 1-based indexes.

    > If you search the forums, this fundamental issue is mentioned thousands of times over years and years.

    > Which leads me to a naive question: Lua is open sourced. Why hasn't anyone in the world just forked the language to be 0-based? Is it not as simple as it sounds? I don't know what goes into designing and compiling a programming language, so it seems like such a trivial fix for what appears to be the #1 issue for every person out there allergic to Lua.

    I wonder how many of those who complain have actually used Lua? Same with all the Perl/PHP haters...

    • dublin 314 days ago
      Lua Rocks. (Yeah, I know...) One-based indexing is more natural, far, less error-prone, reflects real world relationships better (people starting at one, not zero) than zero-based indexing, which has become some kind of purity test for the worst sort of propeller-headed CompSci weenies.

      Many other languages have chosen one-based indexing for the same reasons: all of them are languages that have a high incidence of dealing with real-worrd data and relationships: In addition to Lua (a beautifully elegant language) there's FORTRAN, Julia, Tcl, Awk, Mathematica, Matlab, COBOL, Smalltalk, and quite a few others. The architects of these languages made very deliberate decisions to prefer one-based indexing.

      One-based indexing makes a LOT MORE sense for most real-world work, and as noted, avoids the ubiquitous off-by-one errors that pervade the unnatural zero-based indexing.

  • orbz 315 days ago
    Forking, maintaining, marketing and generally all the community aspects of dealing with such a small change is not worth the effort.

    It’s not the only big 1 indexed language (R, COBOL, Wolfram, Smalltalk, etc.), sometimes as engineers we just have to get used to idiosyncrasies between languages similarly to how bilingual people do the same with spoken language.

  • aspyct 315 days ago
    And break every library there is, just for a minor inconvenience?

    Damn, wait till you work with... Well, just about any language.

  • 1attice 314 days ago
    This obsessive horror with Lua's base-1 array numbering somehow overlooks the far more upsetting fact that `0` and `''` are both truthy in Lua.

    To be clear: I love Lua; but if you're going to have a reaction, please react to the features that are genuinely counterintuitive, rather than (if you will) counter-counterintuitive.

    • FreshStart 314 days ago
      Nil and false are both not truth. It's also just a leaving c conventions, which state anything not 0 is true (& 0XFFFF!= 0) be praised has been expanded to ridulous levels.

      The merge of existence checks and boolean operations in the same operator is wrong.

      • 1attice 314 days ago
        Yes, I knew this, and it's beside the point.

        Which is that, despite loving Lua, I can recognize that it features several 'improvements' which are going to cause Surprise for a lot of coders. Which is bad, following the Law of Least Surprise.

        Personally, I agree with you, but I can also see why Lua initially turns a lot of people off; it took a good year of meeting it in my Neovim config before I accepted that it wasn't some horrible corruption of JavaScript. Now I tend to think of Lua as an improvement over JavaScript: spare, elegant, and almost perfect.

        But I remember that first year, and I can still empathize with people in their first year of Lua.

  • sesuximo 315 days ago
    Why not try it and report back?
  • golergka 315 days ago
    Why would you invest do much effort into making a fork which would completely, absolutely not compatible with the original language?
  • TeddyDD 314 days ago
    If you every decide to create a scripting language, remember - it must look like C. 0-based indexing, syntax, everything. Gods forbid if you write lisp interpreter! Basically, every derivation from C will be treated as heresy and will cause countless complaints from people who never wrote a line of code in your language. /s

    Seriously now: it can be done, but would be a massive waste of time.