News for hackers. Stuff that matters.
Re:Lua, a Misunderstood Language (2021) by bawolff on Thursday September 29, 2022 @11:57AM (#33022474)
I know this is silly, but my biggest complaint is lack of shorthand ++ and += operators.

I can get over 1-based, but i want my ++.

Re:Lua, a Misunderstood Language (2021) by scythe on Thursday September 29, 2022 @12:23PM (#33022863)
+= has a confusing interpretation when the lvalue is a metatable invocation or function result. For readability given the flexibility of Lua's operator overloading, it is always better to be explicit.

The ++ increment operator is even worse, because it leads to statements within statements. Even an experienced C programmer might have trouble understanding what happens when I write `y = x + x++;`. Lua's grammar achieves a simple parse with no statement delimiters because it enforces a distinction between expressions and statements.

Re:Lua, a Misunderstood Language (2021) by bawolff on Thursday September 29, 2022 @01:31PM (#33024244)
All these problems could be solved by just having ++ and += not return a value, which gets rid of all the confusing cases but keeps the useful cases.
Re:Lua, a Misunderstood Language (2021) by UncleEntity on Friday September 30, 2022 @10:15AM (#33035123)
++ is useful to switch over the items of a string:

  switch (x++) {
      case ‘+’:
          if (x == ‘+’) {
              op = OP_INC;
              x++;
          } else if (x == ‘=‘) {
              op = OP_ADDEQ;
              x++;
          } else {
              op = OP_ADD;
          }
          break;
       …
  }
Re:Lua, a Misunderstood Language (2021) by wruza on Thursday September 29, 2022 @02:37PM (#33025197)
It may have a confusing behavior, not interpretation. += may mean “evaluate lvalue, add a value to it as usual and set it back”. If you end up getting confused by:

  t.x = t.x + v
Then it is a question to t’s metatable designer either way.
Re:Lua, a Misunderstood Language (2021) by neutrono on Thursday September 29, 2022 @01:27PM (#33024183)
isn't y = x + x++; undefined behavior in C, because of a lack of a sequence point? If that's true, then it makes sense for no C programmer to know what happens.. I think to fix it you would have to go like: y = x; y = y + x++; I think that would work..
Re:Lua, a Misunderstood Language (2021) by edflsafoiewq on Thursday September 29, 2022 @12:39PM (#33023208)
What's confusing about it?
Re:Lua, a Misunderstood Language (2021) by exac on Thursday September 29, 2022 @12:00PM (#33022525)
FYI These operators are being removed from modern languages now, via style guides, linters, or the language itself (eg: Swift). Personally I actually like the increment and decrement operators, but I understand how they can be confusing (especially ++x vs x++).
Re:Lua, a Misunderstood Language (2021) by Alupis on Thursday September 29, 2022 @12:17PM (#33022782)
This trope needs to die. If you cannot understand ++x vs x++ then you fundamentally do not understand the language - or programming in the more general sense.

Let's stop stupifying our languages for beginners. Beginners are only beginners for a short while, and increasing verbosity with zero gain is annoying at best for everyone else.

Re:Lua, a Misunderstood Language (2021) by mynameisash on Thursday September 29, 2022 @12:35PM (#33023123)
> If you cannot understand ++x vs x++ then you fundamentally do not understand the language

I hardly think this is the issue. The problem is this kind of thing becomes so easy to write but not consciously notice that it causes disproportionately costly mistakes, and I don't think the benefit is worth it.

Python and other languages don't have the increment operator (pre or postfix), but never has this been a problem for productivity in any of my teams. On the contrary, I've encountered several instances where a senior engineer with 20+ years in the industry can't for the life of them figure out why some particular value is off-by-one. It wastes hours of time and often takes a fresh set of eyes to figure it out.

I know a senior principal engineer who wasted an entire day of his time because a comma at the end of the line turned his Python scalar into a unary tuple and was incredibly difficult to notice. "5," is semantically equivalent to "(5,)" but Python isn't strict enough in this instance. It should be strict, though, not for stupifying but for protecting against errors that we all make.

Re:Lua, a Misunderstood Language (2021) by Alupis on Thursday September 29, 2022 @12:52PM (#33023481)
Perhaps the problem isn't really the increment operator, but rather folks writing too fancy of code while using it. One should probably not deeply nest things like increments... because they do get difficult to read. That's not an increment operator problem though, that's an engineer problem.

The example with Python doesn't apply here. We cannot just go around removing language features because sometimes people write bad code.

Further, if you read the code and walk through it in your head, finding a mistaken tuple someplace unexpected should have been easy to find... which means your senior principal engineer isn't very good at reading code, even if they are very good at writing code.

Reading code is it's own skillset I've found... that and apparently the debug steppers for python aren't very good either...

Re:Lua, a Misunderstood Language (2021) by Sohcahtoa82 on Thursday September 29, 2022 @02:07PM (#33024796)
> Perhaps the problem isn't really the increment operator, but rather folks writing too fancy of code while using it

I find this is a common theme of many complaints of C++ and OOP.

For example, C++ lets you do some incredibly ugly things using operator overloading. This can be useful so you could, say, make a matrix class and allow matrix operations to just using + or * rather than .add and .multiply functions. The fact that it can be abused to do very non-sensical, unintuitive things should be a criticism of the programmer who does it, not the feature.

Re:Lua, a Misunderstood Language (2021) by 5d8767c68926 on Thursday September 29, 2022 @01:42PM (#33024425)
>... a comma at the end of the line turned his Python scalar into a unary tuple

I have been burned by this mistake a handful of times. Each instance managed to eat up a shocking amount of time. I definitely consider it a misfeature.

Re:Lua, a Misunderstood Language (2021) by Sohcahtoa82 on Thursday September 29, 2022 @02:10PM (#33024847)
I'm trying to figure out how you ended up with a stray comma at the end of a line. A simple typo, bumping the key without noticing?
Re:Lua, a Misunderstood Language (2021) by Alupis on Thursday September 29, 2022 @02:24PM (#33025042)
My guess would be a bad auto-complete suggestion that wasn't read carefully before accepting.

Loose-type languages often struggle with auto-complete and related. I don't know enough about current state of python development to say with certainty, however.

Re:Lua, a Misunderstood Language (2021) by 5d8767c68926 on Thursday September 29, 2022 @04:15PM (#33026502)
Cannot offer any justification for the error. Just that I have done it more than once, and each time it has taken me too long to debug the underlying cause.
Re:Lua, a Misunderstood Language (2021) by skitter on Thursday September 29, 2022 @12:49PM (#33023420)
++x and x++ just seem so unnecessary. Two extra operators for a very specific case, in which they either safe one character verses x+=1 or mix different mutations into the same line. Iterating a list by incrementing an index doesn't even come up that often if your language supports iterators well.

Edit: Just checked, the project I just had open has 7 instances of +=1 in 5k loc and none of them would have been improved by ++

Re:Lua, a Misunderstood Language (2021) by Alupis on Thursday September 29, 2022 @12:59PM (#33023615)
Well, Lua also does not support += or any of it's counterparts. You have to be extremely literal with x = x + 1, which is just a huge waste for very little gain.

I'm all for beginners learning easier constructs... teach them x = x + 1 if you want, just don't remove the shorter constructs for the rest of us.

Iterating a list/array is just one of many use cases for the increment/decrement operators anyway...

Re:Lua, a Misunderstood Language (2021) by bawolff on Thursday September 29, 2022 @12:13PM (#33022721)
i like x++, but relying on the return value usually tends to be evil.