• pelya
          link
          fedilink
          211 month ago

          In Lua all arrays are just dictionaries with integer keys, a[0] will work just fine. It’s just that all built-in functions will expect arrays that start with index 1.

          • my_hat_stinks
            link
            fedilink
            91 month ago

            That’s slightly misleading, I think. There are no arrays in Lua, every Lua data structure is a table (sometimes pretending to be something else) and you can have anything as a key as long as it’s not nil. There’s also no integers, Lua only has a single number type which is floating point. This is perfectly valid:

            local tbl = {}
            local f = function() error(":(") end
            
            tbl[tbl] = tbl
            tbl[f] = tbl
            tbl["tbl"] = tbl
            
            print(tbl)
            -- table: 0x557a907f0f40
            print(tbl[tbl], tbl[f], tbl["tbl"])
            -- table: 0x557a907f0f40	table: 0x557a907f0f40	table: 0x557a907f0f40
            
            for key,value in pairs(tbl) do
              print(key, "=", value)
            end
            -- tbl	=	table: 0x557a907f0f40
            -- function: 0x557a907edff0	=	table: 0x557a907f0f40
            -- table: 0x557a907f0f40	=	table: 0x557a907f0f40
            
            print(type(1), type(-0.5), type(math.pi), type(math.maxinteger))
            -- number	number	number	number
            
          • @frezik@midwest.social
            link
            fedilink
            41 month ago

            PHP did that same thing. It was a big problem when algorithmic complexity attacks were discovered. It took PHP years to integrate an effective solution that didn’t break everything.

        • db0
          link
          fedilink
          101 month ago

          Fortran angrily starts typing…

          • db0
            link
            fedilink
            151 month ago

            I always felt that Lua was a girl

            • Sonotsugipaa
              link
              fedilink
              English
              201 month ago

              Lua - Portuguese feminine noun for “moon”, coming from the Latin “luna”
              Luna - Latin, feminine noun (coincidentally identical to the Italian noun, also feminine)

              Yup, Lua is a girl.

      • Writing Lua code that also interacts with C code that uses 0 indexing is an awful experience. Annoys me to this day even though haven’t used it for 2 years

      • nickwitha_k (he/him)
        link
        fedilink
        11 month ago

        This is one of the few things that I really don’t like any Lua. It’s otherwise pretty decent and useful.

    • @dan@upvote.au
      link
      fedilink
      51 month ago

      Visual Basic used to let you choose if you wanted to start arrays at 0 or 1. It was an app-wide setting, so that was fun.

        • @dan@upvote.au
          link
          fedilink
          21 month ago

          It’s how I got into programming, so I’ll always have a soft spot for it. Now it’s over 20 years later and I’m still coding.

          • nickwitha_k (he/him)
            link
            fedilink
            11 month ago

            Apple Basic (on an Apple IIe) was my first language that I recall.

            Didn’t have a computer powerful enough for VB until later. It does have a special place in my nostalgia zone but has also led so many astray.

        • Something Burger 🍔
          link
          fedilink
          21 month ago

          Not in languages where you don’t manually handle memory, such as PHP, SQL, Python… Higher-level languages using 0-indexed arrays are letting the abstraction leak.

      • Traister101
        link
        fedilink
        51 month ago

        So what’s 0 do then? I’m okay with wacky indexes (I’ve used something with negative indexes for a end-index shorthand) but 0 has to mean something that’s actually useful. Using the index as the offset into the array seems to be the most useful way to index them.

        • @labsin@sh.itjust.works
          link
          fedilink
          01 month ago

          I’d say the index is actually an offset is a reasoning for explaining why it should start at 1. If index was an index, I’d just start at 1.

          I don’t think any one is better than the other, but history chose 0.

          That you can choose it in VB is probably the worst option :D