[swift-evolution] Reconsider ++ and -- operators removal and prevent other well-known operators from change

davesweeris at mac.com davesweeris at mac.com
Sat Jan 30 15:56:47 CST 2016


My understanding is that while we’re not specifically avoiding “similarity with C”, it’s not one of Swift’s design goals, either. That it’s happened in some areas is really just a side-effect of, a) Swift has to be able to interoperate with C (and Obj-C) code, and b) sometimes there’s nothing wrong with C’s syntax.

I’m “+0.25” towards removing ++ / —. I liked some of the clever code they enabled, but I understand (and agree with) the reasons for their removal. If you want to keep using them, though, they’re trivially easy to put back in:
prefix operator ++ {}
postfix operator ++ {}
prefix func ++ <T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>> (inout x: T) -> T {
    x += 1
    return x
}
postfix func ++ <T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>> (inout x: T) -> T {
    defer { x += 1 }
    return x
}

var i = 0
print(i++) // 0
print(i)   // 1
print(++i) // 2
print(i)   // 2

And if you really prefer C’s behavior, you could even make them silently overflow instead of crashing (just beware the infinite loops…):
prefix func ++ <T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>> (inout x: T) -> T {
    x = x &+ 1
    return x
}
postfix func ++ <T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>> (inout x: T) -> T {
    defer { x = x &+ 1 }
    return x
}
var i:UInt8 = 255
print(i++) // 255
print(i)   // 0

- Dave Sweeris (The Other Dave)

> On Jan 30, 2016, at 10:12, Vanderlei Martinelli via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hello everybody.
> 
> I see Swift as a member of “C family" (a granddaughter/grandson of C, a daughter/son of Objective-C). OK, she/he is different and has her/his own personality like a daughter/son should be and have, but I still like to see Swift and recognize some traces that I know are things that became from C.
> 
> This said, I would like to say that after the removal of `++` and `--` my code becomes less readable and more prone to errors. There were two characters to differentiate an addition from a subtraction, now there is only one (`+= 1`, `-= 1`). Also the character keys are very close in the US keyboard so it is easier to make a mistake and is not as simple as the previous solution when I typed two times the same key. Using Erica's way of saying certain things: I do not love the removal of `++` and `--`.
> 
> I do not know how far the Swift is to the adolescence, but it is certain that teenagers are rebels. There's something very good at it. In most cases they are to be certain. But in some things they regret later. Now I see that many of us want to replace the `??` operator to something else. I'm wondering the next steps...  To replace the `&&`, `||` and `!` operator with `and`, `or`, `not`? I’m not "loving" this as well.
> 
> Are these changes really necessary for the Swift evolution? Is it the better path to deny its origin and to try to fix what is not broken? I would like you to think about it.
> 
> There are many other things that really need to be improved and repaired and other things needed to are created. Those mentioned here in this message does not seem to fit it.
> 
> Regards,
> 
> -Van
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160130/18d658b9/attachment.html>


More information about the swift-evolution mailing list