News for hackers. Stuff that matters.
Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @07:56PM (#23853976)
> indexing with `[1, #list + 1)` isn't a problem _at all_ in Lua.

That's not what string.sub accepts though.

> Plus, between the operations of "get final index" and "split interval into non-overlapping pieces", "get final index" is far more common (it's crucial to stacks, queues, etc, in addition to various uses like iterating backwards). Choosing "closed" intervals is then the cleaner option to avoid an ugly "+" or "-"!

It (rather unnecessarily) takes log time, so you probably shouldn't do it if you can avoid it.

Re:What's New in Lua 5.4 by ufo on Wednesday July 15, 2020 @08:15PM (#23854151)
> It (rather unnecessarily) takes log time, so you probably shouldn't do it if you can avoid it.

This is one of the things that improved in Lua 5.4. Now it caches the table length so that the # operator is O(1) in many common situations.

Re:What's New in Lua 5.4 by ebg13 on Wednesday July 15, 2020 @08:27PM (#23854267)
Will it still give the wrong answer if your table isn't sequentially indexed?

That is, does #({[1]="a", [3]="b"}) return 1 or 2?

edit: I see that it still returns 1. That's a shame. This trips up so many newcomers who've been told that "#" means "length". IMO they need to either start calling it something other than the "length operator" or make it _actually_ return the length. Sadly at this point it would just be yet another breaking change.

Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @08:31PM (#23854303)
1 or 3 but not 2

edit: yes, in fantasy-lua there will be an array type that can represent [1,nil,3] (sequence of 3 values)

Re:What's New in Lua 5.4 by ufo on Wednesday July 15, 2020 @08:57PM (#23854482)
The ideal fix would to allow tables to store nil values, instead of always treating nil as a "hole". Instead of assigning nil to remove a value, you would call a separate "delete" function or operator. This way there would still be only one table type instead of separate types for tables and arrays.

Some early alpha versions of Lua 5.4 played around with this idea but it turns out that it would break too many things. I'd expect that if Lua ever makes a major jump to Lua 6.0 then nils in tables will likely be one of the main changes.

Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @11:07PM (#23855309)
I'd like to read more about this. It would be a little unfortunate to end up with Python's "x in y" and "del y[x]" just to differentiate between missing entries and present entries set to nil.

On the other hand, my not-very-well-considered preferred approach creates some asymmetry between arrays and maps as to what values can be stored. Or it perpetuates the existing asymmetry between varargs and tables...

Re:What's New in Lua 5.4 by pansa2 on Wednesday July 15, 2020 @11:41PM (#23855507)
From http://lua-users.org/lists/lua-l/2018-03/msg00155.html. Looks like this was in the first pre-alpha release of 5.4 but removed shortly afterwards:

> t[i]=undef is a special syntax form that means "remove the key 'i' from table 't'". You can only use 'undef' in three specific forms:

  t[i] = undef     -- remove a key from a table
  t[i] == undef    -- test whether a table has a key
  t[i] ~= undef    -- test whether a table has a key
Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @11:56PM (#23855620)
Thanks! I like this proposal a lot.