[swift-evolution] [Pitch] String prefix operator

Michael Peternell michael.peternell at gmx.at
Sat Jun 18 16:50:08 CDT 2016


> Am 17.06.2016 um 07:45 schrieb Charlie Monroe via swift-evolution <swift-evolution at swift.org>:
> 
> Motivational example:
> 
> var urlString = self.urlString
> if urlString.hasPrefix("//") {
> 	urlString = "http:" + urlString // urlString needs to be typed twice
> }
> 
> While there is currently an easy way to append string using +=, there is no shortcut for prefixing a string. What I propose is adding a =+ operator for prefixing the string:
> 
> urlString =+ "http:"
> 
> Would anyone else find it useful?

No. What I would find useful though, is to recognize that addition is not string concatenation. There is a strong convention in mathematics that the "+" symbol should only be used for operations that are commutative. String concatenation is not commutative. (There are more conventions regarding "+", but all of them are respected by numbers, vectors, complex numbers, quaternions, or matrices - just to name a few.)

I would like to have a different operator for string concatenation. Maybe "~~" or "++"? Now that the prefix and postfix operators for numbers ("++" and "--") are removed from Swift 3, "++" could be used for string concatenation.

-Michael



More information about the swift-evolution mailing list