[Pitch] String prefix operator


(Charlie Monroe) #1

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?


(Michael Peternell) #2

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

···

Am 17.06.2016 um 07:45 schrieb Charlie Monroe via swift-evolution <swift-evolution@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?


(Xiaodi Wu) #3

>
> 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.

I don't see how this would measurably improve Swift code. IIUC, much of the
problem with `+` and strings arises from implicit conversions that don't
happen in Swift. It's not even possible to write a generic algorithm that
accidentally confuses arithmetic `+` and string concatenation `+`, since
you would have to retroactively conform strings and numeric types to a
nonsensical protocol of your own making.

···

On Sat, Jun 18, 2016 at 4:50 PM, Michael Peternell via swift-evolution < swift-evolution@swift.org> wrote:

> Am 17.06.2016 um 07:45 schrieb Charlie Monroe via swift-evolution < > swift-evolution@swift.org>:

Maybe "~~" or "++"? Now that the prefix and postfix operators for numbers
("++" and "--") are removed from Swift 3, "++" could be used for string
concatenation.

-Michael

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Gwynne Raskind) #4

Hi everyone, this is my first post to swift-evolution, so I apologize in advance if I violate any rules of list etiquette, and thanks for reading!

>
> 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.

I don't see how this would measurably improve Swift code. IIUC, much of the problem with `+` and strings arises from implicit conversions that don't happen in Swift. It's not even possible to write a generic algorithm that accidentally confuses arithmetic `+` and string concatenation `+`, since you would have to retroactively conform strings and numeric types to a nonsensical protocol of your own making.

I agree there’s no issue with type confusion, but I do agree with Michael that there’s a conceptual issue with using + for strings. I would certainly favor something that makes it more obvious visually that strings are the operands. An extreme example of where it might be a problem would be a construct like `"2" + "3"` (freely admitted to be a pathological case) - I’d have to look twice to recognize that the result was "23", not "5". My personal favorite for alternate string concat operators is Lua’s ".." syntax, though there are other options as well, especially in Swift. (Speaking for myself, I’d much rather be reminded of Lua than PHP’s single-dot or VB’s ampersand :).

That having been said, the way things are now works and I don’t see the conceptual confusion being an immediate issue; if there are other arguments in favor of changing it, I’d speak out more strongly for it, but taking it as an entirely independent proposal, I'd wonder if it’s worth the break with previous versions in the near future.

With respect to the original suggestion for an operator to prefix a string, I don’t see that as useful enough in the general case to warrant being in the stdlib (relative to how much authority I have to say anything about the stdlib at all! :), especially given that it’s very easy to write your own. I agree that writing a variable name twice is annoying, but given Swift’s predisposition towards let vs. var variables wherever possible, it shouldn’t be that common an issue in my (thus far somewhat limited) experience.

Maybe "~~" or "++"? Now that the prefix and postfix operators for numbers ("++" and "--") are removed from Swift 3, "++" could be used for string concatenation.

-Michael

-- Gwynne Raskind

···

On Jun 18, 2016, at 17:44, Xiaodi Wu via swift-evolution <swift-evolution@swift.org> wrote:
On Sat, Jun 18, 2016 at 4:50 PM, Michael Peternell via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
> Am 17.06.2016 um 07:45 schrieb Charlie Monroe via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>>:


(Erica Sadun) #5

I concur.

I am willing to live with the redundancy in the urlString example for the following reasons:
Like (other) streams, Swift strings prioritize "append" operations. This is how `+` now works.
If we tie ourselves to mathematical convention, Swift becomes a different language. Consider the Swift assignment operator (`=`), which is definitely not commutative. IMO, Swift uses mathematical conventions. Mathematical conventions do not define Swift.
Prefixing does not place such an undue burden on the user that I think it warrants language modification
-- E

···

On Jun 18, 2016, at 4:44 PM, Xiaodi Wu via swift-evolution <swift-evolution@swift.org> wrote:

On Sat, Jun 18, 2016 at 4:50 PM, Michael Peternell via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

> Am 17.06.2016 um 07:45 schrieb Charlie Monroe via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.

I don't see how this would measurably improve Swift code. IIUC, much of the problem with `+` and strings arises from implicit conversions that don't happen in Swift. It's not even possible to write a generic algorithm that accidentally confuses arithmetic `+` and string concatenation `+`, since you would have to retroactively conform strings and numeric types to a nonsensical protocol of your own making.