News for hackers. Stuff that matters.
Re:What's New in Lua 5.4 by blibble on Wednesday July 15, 2020 @06:09PM (#23852805)
I used to quite like Lua, but got quite consistently wound by up several things

    - global variables by defauilt
    - weird choice of certain operators (~= for not equal)
    - arrays start at 1
(fixed yet?)
Re:What's New in Lua 5.4 by ufo on Wednesday July 15, 2020 @08:10PM (#23854109)
I find that the global by default thing is not as much of a problem if using a linter like Luacheck. If the text editor is configured to show linter warnings it catches typos before they become a problem.

https://luacheck.readthedocs.io/

Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @08:15PM (#23854158)
Global by default and block level scope seems like the right choice. In a popular dynamic language that uses local by default and function level scope, the language had to include two keywords for accessing variables other than locals, and the following becomes a bug when it would not be a bug in any sane language:

  x = calculate_n_dogs()
  # later
  ok = all(x.status_code == 200 for x in my_other_responses)
  # later
  let_the_dogs_out(x)
  # oh no, why isn't x n_dogs anymore
Re:What's New in Lua 5.4 by kgm on Wednesday July 15, 2020 @09:18PM (#23854643)
This is clearly about Python, so I will note that this is not actually the case: The generator expression introduces a new scope, and so it does not reassign the value of x. Python does not require the "global" and "nonlocal" keywords in order to access variables, but to reassign names brought in from an outer scope.

Python 2 did have the issue where list comprehensions (though still not generator expressions) didn't introduce a new scope, and so you could indeed get issues quite similar to your example above. This was corrected in Python 3.

All that said, I will agree that Python's scoping rules are pants-on-head crazy. In very nearly every other programming language under the sun, you declare variables at the point where they exist. In Python, you declare a variable when it exists somewhere else (and you then want to reassign it). This does mean that you don't need to put a "local" or "var" or what-have-you in front of each new variable you declare (which is basically the reason Python did it this way), but the semantics of the thing can take some getting used to.

Re:What's New in Lua 5.4 by anonymoushn on Wednesday July 15, 2020 @09:44PM (#23854816)
I appreciate the correction and I'm glad to know this is fixed in python 3!

I've run into similar issues where the variable was used in a for loop declaration rather than a list comprehension, but these are a bit easier to spot. Also, maybe people who don't use lua constantly won't be in the habit of thinking they can safely shadow locals with their looping variables.

Re:What's New in Lua 5.4 by G4BB3R on Wednesday July 15, 2020 @10:04PM (#23854952)
I am impressed modern languages haven't adopted 1-indexed array. This was a design mistake from BCPL and C.
Re:What's New in Lua 5.4 by sitkack on Wednesday July 15, 2020 @11:38PM (#23855494)
Lua was initially designed as a data description language for Oil and Gas simulations written in Fortran. 1 based indexing makes perfect sense in that context.
Re:What's New in Lua 5.4 by idle_zealot on Thursday July 16, 2020 @01:37AM (#23856119)
Having 0-indexed arrays[1] makes sense. It's lists[1] that should be 1-indexed.

[0]: where "array" means contiguous memory.

[1]: where "list" refers to a high-level collection of elements

Re:What's New in Lua 5.4 by Pxtl on Wednesday July 15, 2020 @11:52PM (#23855592)
To me it's the lack of some functional features for 1-liners that really trip me up. Verbose function declaration, no null-coalesce operator, no ternary operators, stuff like that.

I love the platform and ideals and goals - a simple, lightweight embedding language... but the language details rub me the wrong way.

Re:What's New in Lua 5.4 by anonymoushn on Thursday July 16, 2020 @12:24AM (#23855751)
A minor hobby of mine is collecting proposed changes for Lua. Let me make sure I have these right:

- some syntax like js arrow function (this is tough because {} are not block delimiters, maybe single-expression lambdas like in python? I kind of hate those though.)

- `??` operator which does the same thing as `or` except when the left operand is exactly `false`

- `?:` operator. Should this evaluate to exactly one value or any number of values? e.g. x?2,3:4,5

I recently encountered a library that addresses some of the first need: https://github.com/starwing/luaiter#the-selector-interface

Re:What's New in Lua 5.4 by mingodad on Thursday July 16, 2020 @02:58AM (#23856537)
Also there is https://github.com/mingodad/ljs that changed the parser to accept a Javascript like syntax, including https://github.com/mingodad/ljsjit for LuaJit and includes a tool to automatically convert lua to ljs https://github.com/mingodad/ljs/tree/master/lua2ljs and did several project conversions to check that it's feasible and works (see the readme).
Re:What's New in Lua 5.4 by Pxtl on Thursday July 16, 2020 @11:43AM (#23860130)
Exactly.

Theoretically I could get this stuff by using Moonscript, but then I'm using a somewhat esoteric language that brings in a lot of other very unusual ideas.

Although for ternary, I honestly think the "if/elseif/else can be used as an expression" has always been my favorite syntax. Or the CASE...WHEN syntax of SQL.

Re:What's New in Lua 5.4 by anonymoushn on Friday July 17, 2020 @01:54AM (#23867505)
Some compile-to-lua languages include if expressions, but they commonly compile to IIFEs to support evaluating to a number of values other than 1. It's troublesome. I think the bytecode can express what you want.
Re:What's New in Lua 5.4 by scythe on Thursday July 16, 2020 @02:15AM (#23856315)
>no null-coalesce operator

`or` does this unless there is a possibility your function returns boolean `false`

Re:What's New in Lua 5.4 by ebg13 on Wednesday July 15, 2020 @06:15PM (#23852898)
~= and 1-indexing are only weird if you haven't seen them before. MATLAB uses them as well.
Re:What's New in Lua 5.4 by samatman on Wednesday July 15, 2020 @09:31PM (#23854745)
I work with Lua extensively, including abstract syntax trees.

To mark a node at the third position, containing one character, we say {3,3}.

But what if there's a node at the third position, that contains no character? Well, thats {3,2}.

It's like nails on a chalkboard every time. 1-based indexing is a mistake.

~=, by contrast, is just notation. You get used to it quickly.

Re:What's New in Lua 5.4 by saagarjha on Wednesday July 15, 2020 @09:44PM (#23854823)
> To mark a node at the third position, containing one character, we say {3,3}. > But what if there's a node at the third position, that contains no character? Well, thats {3,2}

Why not shift over indexing by one consistently? So you'd use {3, 4} and {3, 3} for each, so the length property is preserved.

Re:What's New in Lua 5.4 by samatman on Wednesday July 15, 2020 @10:07PM (#23854965)
Because that's not how the string library works.

("12345"):sub(3,3) returns "3", while ("12345"):sub(3,4) returns "34", and ("12345"):sub(3,2) returns "".

Indexing in the array portion of tables, and strings, is at least consistent; but Djikstra was right.

The Wiki[0] has a good discussion of Djikstra's iconic essay. The best of these arguments, imho: 0 based indexing unifies enumeration and measurement.

[0]: https://wiki.c2.com/?WhyNumberingShouldStartAtZero

Re:What's New in Lua 5.4 by int_19h on Thursday July 16, 2020 @02:57AM (#23856532)
That's an argument against end-inclusive ranges more so than against 1-based indexing. If they were end-exclusive, like in Python, then {3,3} would be an empty string, and {3,4} would be a single character at 3. Note how it suddenly doesn't matter at all whether the indices are 0-based or 1-based - all of the above is valid regardless.

FWIW, I think that the real mistake was to make inclusivity/exclusivity implicit to begin with. If the syntax is explicit, and the choice is captured in the resulting value (i.e. it's more than just a pair of numbers), then you just use whatever is more convenient for the task at hand. Nim almost gets there: (x .. y) is end-inclusive, and (x ..< y) is end-exclusive - but the syntax still exhibits a preference.

Re:What's New in Lua 5.4 by pwdisswordfish2 on Thursday July 16, 2020 @03:26AM (#23856670)
Thing is, end-exclusive ranges go hand-in-hand with zero-based indexing. I don’t think I need to link the EWD article.
Re:What's New in Lua 5.4 by int_19h on Thursday July 16, 2020 @05:56AM (#23857380)
I don't find it particularly persuasive. End-exclusive and end-inclusive can both be natural in different scenarios, regardless of the base - the article simply cherry-picks the ones that it needs to "prove" its case. That's why it's best when the language just offers both.
Re:What's New in Lua 5.4 by pansa2 on Thursday July 16, 2020 @03:54AM (#23856794)
> Nim almost gets there: (x .. y) is end-inclusive, and (x ..< y) is end-exclusive - but the syntax still exhibits a preference.

Swift is similar, using `...` and `..<`. Rust uses an alternative convention, `..=` and `..`.

Would you prefer to see `..=` and `..<` so that inclusivity/exclusivity is always explicit?

Re:What's New in Lua 5.4 by int_19h on Thursday July 16, 2020 @04:23AM (#23856934)
Yep, pretty much. I'm a strong proponent of "explicit is better than implicit" in all cases where there's no default that's obviously preferable, and this is is one of those cases - that different languages picked different defaults is all the evidence I need.
Re:What's New in Lua 5.4 by deathanatos on Wednesday July 15, 2020 @06:43PM (#23853251)
It's not just weird, or different, though; there are reasons to prefer 0-based indexing: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
Re:What's New in Lua 5.4 by curtisf on Wednesday July 15, 2020 @07:39PM (#23853838)
I really don't like seeing this cited as a "proof" that 0-based indexing of lists is better. It is too shallow and ignores too many important facets of the question.

The main contrast Dijkstra is drawing is between closed and half open intervals. Typically, the choice "starting from one" is tied to "closed" intervals, but that's not strictly necessary. Nothing stops you from using half-open intervals on Lua lists any more than you can used closed intervals to index into C arrays.

The main advantage of the typical coupling is avoiding `+`s and `-`s in your "typical" loops over the first n indices:

    for i = 1, n do -- closed
    for (int i = 0; i < n; i++) { // half open
but what if you want to go in reverse? Getting the _final_ indexes requires arithmetic with half-open intervals, but it doesn't with closed (which also requires changing the operator from exclude-eq to include-eq):

    for i = n, 1, -1 do -- closed
    for (int i = n - 1; i >= 0; i--) { // half open
Of course, probably the most important property of half-open intervals is the way that they can be broken down into two disjoint intervals: `[A, C) = [A, B) U [B, C)`. But if your indexes are integers this is straightforward with closed intervals too: `[A, C] = [A, B - 1] U [B, C]` or `[A, C] = [A, B] U [B + 1, C]`.

But again, if you're using this property, you probably don't actually care about the "absolute" indexes at all; indexing with `[1, #list + 1)` isn't a problem _at all_ in Lua.

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 "-"!

Dijkstra mentions that requiring -1 to refer to an initial interval as "ugly" since -1 is not a valid index, but he has no qualms with using the length of the list, n, and allowing it to not be an index!

This is all said without even going into the awkwardness of having [k] being the {k+1}th element (e.g., [4] is how we write "fifth" in C -- I distinctly remember at least one conversation at work where the wrong thing was communicated because someone said "fourth" to mean [4])

Now, don't take this as a serious argument that 0-indexed-lists are inferior to 1-indexed lists. It's just a demonstration that it's easy to argue either side. That's because it's really just an unimportant convention. Pick one and stick with it and everything will work fine, modulo confusion about what "fourth" means.

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.
Re:What's New in Lua 5.4 by a1369209993 on Wednesday July 15, 2020 @09:44PM (#23854818)

  > for (int i = n - 1; i >= 0; i--) { // half open
Well, there's your problem right there[0]; it should be:

  for(size_t i = n; i-- > 0 ;) {
> This is all said without even going into the awkwardness of having [k] being the {k+1}th element

You mean the awkwardness of humans using "kth" to refer to element number k-1?

0: /sarcasm

Re:What's New in Lua 5.4 by mingodad on Thursday July 16, 2020 @02:52AM (#23856504)
Your example doesn't hold water, size_t is unsigned so the test condition would never be met.
Re:What's New in Lua 5.4 by anonymoushn on Thursday July 16, 2020 @03:28AM (#23856685)
Re:What's New in Lua 5.4 by sk0g on Wednesday July 15, 2020 @06:26PM (#23853041)
Isn't "weird == not seen it before" anyway? I get what you're trying to say, but MATLAB is not really a programming language SWEs would use, so if that's the best example you can think of, Lua's bound to confuse most people in the industry.

May work better for mathematicians, non-SW engineers etc, but I wouldn't know.

Re:What's New in Lua 5.4 by ebg13 on Wednesday July 15, 2020 @06:34PM (#23853148)
> I get what you're trying to say, but MATLAB is not really a programming language SWEs would use

It may not be a language that website jockeys use, but "I don't use it therefore probably no one does" is almost never correct. MATLAB is huge in state machine and dynamic systems programming as well as being the original commercial powerhouse for linear algebra algorithms.

Re:What's New in Lua 5.4 by sk0g on Wednesday July 15, 2020 @09:46PM (#23854839)
I meant to say most SWEs, which you seemed to have inferred anyway.

Why do you think that's wrong? I simply claimed MATLAB is much more commonly used by scientists and non-SW engineers. Most SWE jobs are in B2B, web development, embedded, etc fields. Which one of these sees a lot of MATLAB usage?

Maybe don't resort to insults straight away too.

Re:What's New in Lua 5.4 by ebg13 on Thursday July 16, 2020 @12:52PM (#23860874)
> Most SWE jobs are in B2B, web development

Citation?

> embedded, etc fields. Which one of these sees a lot of MATLAB usage?

Both embedded and etc. MATLAB is used widely in signal processing and controls software.

> Maybe don't resort to insults straight away too.

Sorry. I get annoyed when someone pretends that Software==Websites as if any of our trusted electronics are designed or programmed in javascript.

Re:What's New in Lua 5.4 by rpdillon on Wednesday July 15, 2020 @06:28PM (#23853078)
Julia is 1-indexed as well.
Re:What's New in Lua 5.4 by luhn on Wednesday July 15, 2020 @07:58PM (#23853998)
Which is also targeted towards scientific computing.
Re:What's New in Lua 5.4 by jontro on Wednesday July 15, 2020 @06:42PM (#23853246)
The tilde ~ char is pretty annoying to type on swedish keyboards at least. Alt + ¨ and a space on my mac. Having to type this regularly on my main languages would be off putting
Re:What's New in Lua 5.4 by saagarjha on Wednesday July 15, 2020 @09:43PM (#23854805)
> weird choice of certain operators (~= for not equal)

It's a little unconventional, but then again ~ is a fairly standard (bitwise) negation operator.

> arrays start at 1

Lua doesn't do arrays, it does tables. Though they have similar syntax in certain cases, and are occasionally used for the same things, they are fundamentally different things.