News for hackers. Stuff that matters.
Re:Berry is a ultra-lightweight dynamically typed embedded s... by zdragnar on Saturday October 07, 2023 @12:09PM (#37802898)
Having a lot of experience wrangling prototypes in JS was a nice benefit to my first forays into lua, though I wrote more functional code than OO anyway.

Honestly, off by one errors due to indexing was a lot harder to shake than getting used to meta tables.

Re:Berry is a ultra-lightweight dynamically typed embedded s... by runlaszlorun on Saturday October 07, 2023 @01:11PM (#37803471)
I’m assuming that the indexing off by one errors were due to Lua 1-based indexing? If so, was it due to you being used to 0-based indexing or something else?

Just curious. I’ve been kicking around some attempts to make programming simpler (though hopefully no less powerful) for casual programmers. Lua’s 1-based indexing gets a lot of critiques and, while I haven’t written a ton of Lua, can’t help but think that the critiques are due to us all being used to 0-based indexing. Which is a valid critique, especially for a scripting language, but one that applies less to novices.

Re:Berry is a ultra-lightweight dynamically typed embedded s... by norir on Saturday October 07, 2023 @01:31PM (#37803672)
This is somewhat inefficient but zero indexed strings can easily be simulated in lua:

  local array = function(backing)
    local res = { len = function() return #backing end }
    setmetatable(res, {
      __index = function(_, k) return backing[k + 1] end,
      __newindex = function(_, k, v) backing[k + 1] = v end
    })
    return res
  end
  local x = array { 1, 2, 3 }
  print(x[0]) -- 1
  x[1] = 5
  x[x.len()] = 6
  for i = 0, x.len() - 1 do print(x[i]) end -- 1\n5\n3\n6
Re:Berry is a ultra-lightweight dynamically typed embedded s... by zdragnar on Saturday October 07, 2023 @01:45PM (#37803839)
Bingo. All of the languages I have used prior to (and since) use 0-indexed arrays.

There's times where doing math with array indexes is simpler with 0 and with 1-indexed arrays. I don't think I really found 0 to be difficult to learn; there are a great many more challenging things.

I wouldn't say "don't use a 1 indexed language" but I've also never found the arguments in favor of it to be compelling when using lua.