11
horus
1y

In Lua arrays start with 1 by default. Please kill me.

Comments
  • 1
    That's a fair abstraction IMO: hiding a pesky implementation detail to prevent off-by-one errors. I wonder why JS doesn't do this all things considered?
  • 1
    So? Arrays don't really have to start from any specific indice
  • 8
    @SuspiciousBug Off-by-one errors are prevented by strong standards. While starting lists from 1 may improve the experience of people who never have and never will program in another language, everyone switching to and from Lua gets surprised. Few people apart from total newbies actually think while writing a for loop; it's just an atom to most of us. Having to write that atom differently in Lua than in every other language is an off-by-one error factory.
  • 2
    You'd enjoy Matlab then
  • 5
    @ALMST your age started at 1 and stayed there ever since, jase
  • 3
    @ALMST That is why the day starts at 1:01:01 and not at 0:00 ?

    GND has a voltage of 1V, not 0V.

    A distance of 1 km is actually nothing, if you want to drive 0 km you have to drive backwards.

    If you have no money in your bank account, you have 1€ + 1ct, not 0€.
  • 3
    @SuspiciousBug I would argue a lot more off by one errors are caused by arrays starting whit 1 rather than 0.

    From a mathematical point of view, 0 makes sense. To be honest, from every point of view 0 makes the most sense.

    They could also start at index π
  • 8
    @happygimp0 index starting at π would be irrational
  • 2
    @happygimp0 you are mixing cardinals and ordinals.
  • 2
    @happygimp0 as a certified mathematician, I can assure you that math does not give any more sense to counting from zero. And to be fair, the most common counting in math is from one. This is exactly why Matlab uses this as well, to not confuse mathematicians who are not programmers.

    Counting from zero is only a thing because of the index being a literal offset from the _first_ element in an array.
  • 1
    @iiii Because &(a[n])==&(*(a+n))
  • 0
    @happygimp0 yes, if using C syntax
  • 1
    @happygimp0 That applies universally to C, and in some cases C++. Should all languages use === for equality comparison too because [] == 0?
  • 1
    @iiii also in asm syntax, which in the end is directly feeded to the cpu
  • 1
  • 1
    @lorentz 0 == ( [] == 0)
Add Comment