Operators ++ and -- have been removed since Swift 3
When first open our projects in Xcode 7.3, probably we would encounter the following warnings:'++' is deprecated: it will be removed in Swift 3
or '--' is deprecated: it will be removed in Swift 3
.
Here is the official explanation of why removing the operators.
Advantages of These Operators
- The primary advantage of these operators is their expressive capability. They are shorthand for (e.g.)
x += 1
on a numeric type, orx.advance()
on an iterator-like value. When the return value is needed, the Swift+=
operator cannot be used in-line, since (unlike C) it returnsVoid
. - The second advantage of Swift supporting this family of operators is continuity with C, and other common languages in the extended C family (C++, Objective-C, Java, C#, Javascript, etc).People coming to Swift from these other languages may reasonably expect these operators to exist. That said, there are also popular languages which have kept the majority of C operators but dropped these (e.g. Python).
Disadvantages of These Operators
- These operators increase the burden to learn Swift as a first programming language - or any other case where you don’t already know these operators from a different language.
- Their expressive advantage is minimal -
x++
is not much shorter thanx += 1
. - Swift already deviates from C in that the
=
,+=
and other assignment-like operations returnsVoid
(for a number of reasons). These operators are inconsistent with that model. - Swift has powerful features that eliminate many of the common reasons you’d use
++i
in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include thefor-in
loop, ranges,enumerate
,map
, etc. - Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage “overly tricky” code which may be cute, but difficult to understand.
- While Swift has well defined order of evaluation, any code that depended on it (like
foo(++a, a++)
) would be undesirable even if it was well-defined. - These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.
What we do
- Just drop these operators entirely, both prefix and postfix.
- Use
x += 1
orx -= 1
instead. - And take care of the places where we using the return value of
++x
or--x
carefully.