Skip to content →

RIP++

When I recently had occasion to “prepare” for Swift 3.0 a third-party library our team is using, it struck me just how useful the C-based post-increment (and decrement) operator is. Consider this simple Swift 2.2 line from that library:

return (String(self.arrayIndex++), JSON(o))

To those not familiar with this post-increment unary operator, the arrayIndex doesn’t get incremented until after it is converted to a String. With the loss of that operator in Swift 3.0, this one-liner grows to this:

let arrayIndexString = String(self.arrayIndex)
self.arrayIndex += 1
return (arrayIndexString, JSON(o))

Can’t say I agree with the stated “disadvantages” of this operator, which appear to focus on a very simplistic use-case that contrasts x += 1 and x++. Sure, in that case, there’s no added utility. And hard to learn? I’ve seen that argument repeated in other “do away with this” arguments; hog wash. Swift contains much more complicated constructs than a post-increment operator!

Alas, I’m a bit late to the party, and the fate of ++ has already been sealed. Thankfully, it’s not “goodbye,” but just “see you later”…when I return to the likes of C++.

Published in Swift Language

Comments

Leave a Reply

Your email address will not be published.