28

I just switch to Swift for couple days and I notice that the postfix and prefix ++ and -- will be removed in Swift 3. I have done some research and according to the announcement on the Swift website, the operator ++ and -- will be replaced by += 1 and -= 1. Link here New Features in Swift 2.2

I have a piece of code that work just fine with the old version of Swift. When I change from return counter1++ which is my original code to return counter1 += 1 and an error pops up saying

No '+=' candidates produce the expected contextual type 'Int'

Here is my example

func countingCounter() -> (() -> Int){
    var counter1 = 0
    let incrementCounter1: () -> Int = {
        return counter1+=1   //original is counter1++
    }
return incrementCounter1
}

I have tried to work this out but still stuck.

idmean
  • 14,540
  • 9
  • 54
  • 83
hientrq
  • 762
  • 8
  • 14
  • 2
    i don't know swift but here is what i guess : in this language `+=` expression don't return value so you can't `return couter1+=1; you have to do `counter+=1` and then `return counter1` – Walfrat Apr 26 '16 at 06:47
  • 2
    @Walfrat this almost justifies why `++` might disappear ... in this case his `return counter1++` is really doing `counter2 = counter1; counter1 += 1; return counter2`. Postfix is often an error waiting to happen ... – donkopotamus Apr 26 '16 at 06:51
  • 1
    yes returning a postfix/prefix ++ is usally not a good idea, using them should be reserved for true performance purpose with developers that really know what they're doing. – Walfrat Apr 26 '16 at 07:01
  • 1
    arf forgot the postifx thing. You have to do `counter1+=1` `return counter1-1` – Walfrat Apr 26 '16 at 07:19
  • @Walfrat Thank you. That helps. I just wonder how to mark that your answer correct because I don't see the little green tick here – hientrq Apr 26 '16 at 07:27
  • You can't, i post and answer :) – Walfrat Apr 26 '16 at 07:30
  • Apple considers removal of functionality to be a feature? – JAB Apr 26 '16 at 13:48

5 Answers5

18

It clearly states in the Apple docs, copied here for your convenience:

NOTE

The compound assignment operators do not return a value. For example, you cannot write let b = a += 2.

No, the ++ operator is NOT the same as +=.

Community
  • 1
  • 1
Laffen
  • 2,753
  • 17
  • 29
9

As said in my comment here is how you have to write it now to replace the postfix ++. ANother way would be to use an intermediary variable if you don't like the -1 thing.

let incrementCounter1: () -> Int = {
    counter1+=1   //original is counter1++
    return counter1-1;
}
Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • Why are you returning `counter1-1`? The function is supposed to increment the value, not do nothing – LordAro Apr 26 '16 at 14:45
  • 3
    this function is supposed to return the currentValue and increment it by 1 for the next execution. This si typicaly the usage of ++ postfix, but since it has been removed, you have to increment the counter and return the precedent value. – Walfrat Apr 26 '16 at 14:47
  • @LordAro: this can be quite confusing to some people. I suggest you go ahead and check out the different between the use of `value++` and `++value`. If you just return `counter1`, the equivalent would be `return ++counter1` and this is not what I want. – hientrq Apr 27 '16 at 02:46
  • I see it, I didn't notice the closure is all. Thanks for clearing it up though – LordAro Apr 27 '16 at 05:22
8

No, they aren't identical, ++ and -- increment (or decrement) and return, += and -= add (or subtract) an amount in a simple to read short form, but they don't return anything.

So, you need to separate the value change from the value usage. Which is the whole point of the deprecation really - to make your code easier to read and maintain.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

No they aren't identical, you have to increment first, then return the value.try this.

func countingCounter() -> (() -> Int){
    var counter1 = 0
    let incrementCounter1: () -> Int = {
        counter1 += 1
        return counter1
    }
    return incrementCounter1
}
NuttLoose
  • 665
  • 7
  • 11
  • 2
    yeah but I think you should `return counter1 - 1` because of the prefix/postfix things. If you run some tests you will see it provides different results – hientrq Apr 26 '16 at 07:30
0

Practicing necromancy here, but nobody mentioned defer yet - here's a slightly more elegant alternative to @Walfrat's method you may like the look of. Defer does the incrementing after returning the value.

let postIncrementCounter1: () -> Int = {
    defer { counter1 += 1 } // original is counter1++
    return counter1
}
Mete
  • 5,495
  • 4
  • 32
  • 40