News for hackers. Stuff that matters.
Re:Implemented proposals for Swift 3 by kinofcain on Saturday May 07, 2016 @12:19PM (#11650155)
Rust did too. There's probably a pie chart somewhere of time lost to having to type out "foo += 1" vs number of hours lost to people screwing up operator precedence for foo++ or ++foo. (EDIT: as munificent points out this is actually a combination of operator precedence and sequence points)

The pie chart is probably severely lopsided.

EDIT: Since the main problem is the precedence of the post increment/decrement operator, I would have preferred to see them get rid of that one and keep ++foo but overall not much of a loss.

At least they didn't get rid of the ternary operator like Go did...

Re:Implemented proposals for Swift 3 by munificent on Saturday May 07, 2016 @01:06PM (#11650354)
It's not about operator precedence, it's about sequence points. Assignment—including in ++—is a side-effecting operation. An expression form that does side effects can lead to very confusing code like:

    foo(a++, a++);
What values get passed foo()? In some languages, it's undefined (!). In others, there is a well-defined answer, but even there the answer may not be intuitive. Removing assignment expression forms avoids this. Note that even "=" is a statement in Go.

It does sacrifice a little expressiveness. But in languages that have dedicated syntax like ranges for iterating over sequential numbers, the ++ and -- operators end up pretty rarely used so it's no big loss.

Re:Implemented proposals for Swift 3 by bjz_ on Saturday May 07, 2016 @09:40PM (#11652199)
> It does sacrifice a little expressiveness. But in languages that have dedicated syntax like ranges for iterating over sequential numbers, the ++ and -- operators end up pretty rarely used so it's no big loss.

This is one of the big points too, that often gets overlooked. The removal of ++ fits nicely with the removal of C-style for loops.

Re:Implemented proposals for Swift 3 by andy_ppp on Saturday May 07, 2016 @07:14PM (#11651809)
In my opinion the example given is awful code, yes we should protect developers from themselves but good taste in code is important and incrementing a counter easily should still be a thing. Just stop the bad cases then rather than the well understood and expected usage.
Re:Implemented proposals for Swift 3 by kinofcain on Saturday May 07, 2016 @01:56PM (#11650588)
Yes, sorry, I should have said sequence points also.
Re:Implemented proposals for Swift 3 by masklinn on Saturday May 07, 2016 @01:23PM (#11650426)
> Note that even "=" is a statement in Go.

In Python and Rust as well.

Re:Implemented proposals for Swift 3 by dbaupp on Saturday May 07, 2016 @11:02PM (#11652430)
Nitpicking: assignment is an expression that returns () in Rust, meaning `foo(a = 1, a)` is valid if foo has a signature like `fn foo(x: (), y: i32)`. However, arguments are (mostly) defined to be evaluated from left to right.
Re:Implemented proposals for Swift 3 by mmastrac on Saturday May 07, 2016 @12:22PM (#11650163)
If anything it would be time lost to accidentally typing ++x instead of x++. I always found the precedence to be pretty intuitive in C (unary ops generally bind tightest). The only time you'd have to Google is if you were doing something weird like `-(x++)`.

I'm also willing to accept that I'm old, stubborn, and unhappy about changing my ways and that this is the future... :)

Re:Implemented proposals for Swift 3 by kinofcain on Saturday May 07, 2016 @12:28PM (#11650192)
See I always thought most people who wrote foo++ actually meant ++foo and it typically only came up when the operator precedence made the two not equivalent, causing a subtle bug.

I can't even think of a time where I really needed a use-then-increment operator where I wouldn't have just used the value, then incremented it on the next line anyway (maybe even with a post-increment out of habit).

Re:Implemented proposals for Swift 3 by stormbrew on Saturday May 07, 2016 @02:19PM (#11650688)

    int arr[5];
    int i = 0;
    while (shouldIKeepGoing() && i < 5) arr[i++] = getAThing();
If you use ++i there you'll skip 0 and overflow the array. This is what the postfix form is for, if you value terseness.

Edit: To clarify, I'm not necessarily saying you don't know that's what it's for, but this is a pattern I see pretty frequently in the wild so I'm just throwing it out there with an explanation of why people find value in it.

Re:Implemented proposals for Swift 3 by mmastrac on Saturday May 07, 2016 @12:50PM (#11650282)
Interesting. I generally choose x++ for the majority of array-striding code I write, and usually as a shortcut to avoid having a bunch of x++ lines scattered around. I've found that you can structure your code either way and postfix seems to be easier to reason over for me.

I use ++x very infrequently, and mainly in hand-written text parsers.

Re:Implemented proposals for Swift 3 by kinofcain on Saturday May 07, 2016 @02:09PM (#11650649)
Well, I guess we're both going to do it the same way in swift 3, which is probably the whole point...
Re:Implemented proposals for Swift 3 by sbmassey on Saturday May 07, 2016 @01:36PM (#11650485)
I generally assume that ++x is idiomatic in c++, and x++ in c, because in c++ you may be calling against some non-primitive type, where the update-and-return-the-non-updated-value semantics of the postfix version are not optimisable away.
Re:Implemented proposals for Swift 3 by boulos on Saturday May 07, 2016 @10:06PM (#11652271)
I generally agree, except in the case of more than one per statement / expression as munificent pointed out above. What does:

  int x = 0;
  printf("%d %d", x++, x++);
print? Is that true for all systems and compilers? (Answer: nope!).
Re:Implemented proposals for Swift 3 by saturncoleus on Sunday May 08, 2016 @01:36AM (#11652775)
If you don't care about side effects:

C:

    int foo = bar ? 2 : 4;
Go:

    foo := map[bool]int{true:2, false:4}[bar]
Python had a similar trick before 2.5 added ternary conditionals.