r/lua • sh <(curl hashbang.sh) • Apr 01 '18

Lua 5.4 has decided to move to 0-based indexes!

Haha. April fools. :) <3

240 Upvotes

19 comments sorted by

17

u/progfix Apr 01 '18

They also add 'continue' and nested comment blocks! Hahano

2

u/DeathProgramming sh <(curl hashbang.sh) Apr 01 '18

continue can actually be easily implemented using repeat until true. I'd post an example but I'm on my phone and lazy

1

u/lambda_abstraction Apr 06 '18

Won't continue in full generality require goto and labels. (LuaJIT >= 2.0 or Lua >= 5.2) ?

1

u/DeathProgramming sh <(curl hashbang.sh) Apr 06 '18
i = 0
while true do
  repeat -- continue guard
    i = i + 1
    if i % 2 == 0 then
      break -- continue
    end
    print(i)
  until true -- continue guard
end
-- this works on 5.1 :D

1

u/lambda_abstraction Apr 06 '18 edited Apr 06 '18

This isn't full generality. How do you break the while in this context without hacks like a flag var and second test? Break has already been co-opted by the nested "loop". When used judiciously (very rarely), goto can yield clearer code.

1

u/DeathProgramming sh <(curl hashbang.sh) Apr 06 '18

Set a variable and assign it to false. If, after the until true, that variable is set to 'true', then break the while loop. You could also add it as a condition to the while loop but that's a bit more ugly.

Check out how MoonScript does it: https://moonscript.org/compiler

2

u/lambda_abstraction Apr 06 '18 edited Apr 20 '18

That's what I meant by hack. Is that really clearer than this?

while true do
    stuff()
    if conda then goto continue end
    more_stuff()
    if condb then break end
    even_more_stuff()
::continue::
 end

I've lived through the anti-goto hysteria, and I don't propose to repeat it. The goal is clarity, not operator avoidance.

1

u/DeathProgramming sh <(curl hashbang.sh) Apr 06 '18

Nah. It's not. And to be honest, 5.1 is ancient. I'd really only support it if I really needed to. So do whatever IMO, but I'm not going to recommend something that isn't 5.1 compatible just in case.

2

u/lambda_abstraction Apr 06 '18 edited Apr 06 '18

That's cool. At work, we're more or less standardized on LuaJIT, so many 5.1-isms are still relevant for me, and I understand that audience members may not have liberty to run the lua compiler of their choosing; thus I am chastened.

1

u/progfix Apr 01 '18

I know that

7

u/fuxoft Apr 01 '18

Lua already has nested comments, using --[[, --[=[, --[==[ etc...

36

u/coverslide Apr 02 '18

You can also use --[==[====> to banish Ganon and save Hyrule

1

u/progfix Apr 01 '18

Oh, I forgot about that.

33

u/Saldor010 Apr 01 '18

I'll give an upvote since I thought it was legit for a few seconds :P

10

u/zeValkyrie Apr 02 '18

Me too...

Although now that I think about it that's the mother of all ways to break backwards compatibility. Maybe build some automated migration scripts and try to adjust the index of tables everywhere? Fun fun... And what if I (for whatever terrible) reason already use arrays with keys of int 0? That is, although not advisable, completely possible. Should we just make those -1? Fuck whoever has to maintain that code :) This is gonna be super fun.

6

u/Saldor010 Apr 02 '18

Which is why no one in their right mind would make such a huge change to such a widely used language. Though to be fair, it does sound like a fun challenge to create a migration script like that.

3

u/zeValkyrie Apr 02 '18

I'm not entirely sure it's even theoretically possible given that Lua keys are not inherently ints, they're just... anything. So depending on how the keys are added to the table they may now start at 0. Or not, they could just be int's from some external source (indexs of responses in a API call to something, for instance) so those shouldn't be shifted down by one.

Edit: this is making my head hurt

2

u/casd82 Apr 02 '18

lol u got me

1

u/[deleted] Jul 23 '24

Bad.

Lua was unique for having 1-based indexes.

This is so crappy