Swift strings are value types, just like C++ or C#. The value type contains a reference to the string text. (Ok, libc++ in llvm will actually tag the pointers on 64 bit platforms to stuff small strings *into* the struct directly, but I digress)
Swift strings are copy on write, where the reference count is used to determine if you possess a unique reference to the string before mutation. This is similar to how libstdc++ works, or at least used to work.
I believe C++ libraries have been migrating away from CoW strings because of the atomicity costs of the reference counting. Swift, maintaining compatibility with Objective C, already needed to have support for ref-counted strings.
So it was a different trade-off - the ref-counted Swift strings can be bridged to Objective C, while a non-reference-counted string would need to be copied. If they make the decision to have reference counted strings to make the interoperability faster, then they might as well add CoW support to make them more efficient/faster in other cases.
Why are strings passed by value? (Score:4, Interesting)
Strings are immutable pass-by-reference objects in most modern languages. Why did you make this decision?
Re:Why are strings passed by value? (Score:1)
Swift strings are value types, just like C++ or C#. The value type contains a reference to the string text. (Ok, libc++ in llvm will actually tag the pointers on 64 bit platforms to stuff small strings *into* the struct directly, but I digress)
Swift strings are copy on write, where the reference count is used to determine if you possess a unique reference to the string before mutation. This is similar to how libstdc++ works, or at least used to work.
I believe C++ libraries have been migrating away from CoW strings because of the atomicity costs of the reference counting. Swift, maintaining compatibility with Objective C, already needed to have support for ref-counted strings.
So it was a different trade-off - the ref-counted Swift strings can be bridged to Objective C, while a non-reference-counted string would need to be copied. If they make the decision to have reference counted strings to make the interoperability faster, then they might as well add CoW support to make them more efficient/faster in other cases.