- Proposal: SE-0089
- Author: Austin Zheng, Brent Royal-Gordon
- Status: Accepted (Rationale)
- Review manager: Chris Lattner
- Revision: 2
- Previous Revisions: 1
Swift's String
type ships with a large number of initializers that take one unlabeled argument. One of these initializers, defined as init<T>(_: T)
, is used to create a string containing the textual representation of an object. It is very easy to write code which accidentally invokes this initializer, when one of the other synonymous initializers was desired. Such code will compile without warnings and can be very difficult to detect.
Discussion threads: pre-proposal part 1, pre-proposal part 2, review thread, post-review thread
String
ships with a number of initializers which take a single unlabeled argument. These include non-failable initializers which create a String
out of a Character
, NSString
, CharacterView
, or UnicodeScalarView
, initializers which build a string out of a number, and failable initializers which take a UTF8View
or a UTF16View
.
There are at least two possible situations in which a user may write incorrect code which nevertheless compiles successfully:
- The user means to call one of the non-failable initializers besides the
init<T>(_: T)
initializer, but passes in an argument of incorrect type. - The user means to call one of the failable initializers, but accidentally assigns the created object to a value of non-nullable type.
In both cases the compiler silently infers the use of the init<T>(_: T)
initializer in lieu of the desired initializer. This may result in code which inadvertently utilizes the expensive reflection machinery and/or produces an unintentionally lossy representation of the value.
A proposed solution to this problem follows:
-
The current reflection-based
String.init<T>(_: T)
initializer will be renamed toString.init<T>(describing: T)
. This initializer will rarely be invoked directly by user code. -
A new protocol will be introduced:
LosslessStringConvertible
, which refines/narrowsCustomStringConvertible
. This protocol will be defined as follows:protocol LosslessStringConvertible : CustomStringConvertible { /// Instantiate an instance of the conforming type from a string representation. init?(_ description: String) }
Values of types that conform to
LosslessStringConvertible
are capable of being represented in a lossless, unambiguous manner as a string. For example, the integer value1050
can be represented in its entirety as the string"1050"
. Thedescription
property for such a type must be a value-preserving representation of the original value. As such, it should be possible to attempt to create an instance of aLosslessStringConvertible
conforming type from a string representation.A possible alternate name for this protocol is
ValuePreservingStringLiteral
. The core team may wish to choose this name instead, or another name that better describes the protocol's contract. -
A new
String
initializer will be introduced:init<T: LosslessStringConvertible>(_ v: T) { self = v.description }
. This allows theString(x)
syntax to continue to be used on all values of types that can be converted to a string in a value-preserving way. -
As a performance optimization, the implementation of the string literal interpolation syntax will be changed to prefer the unlabeled initializer when interpolating a type that is
LosslessStringConvertible
or that otherwise has an unlabeledString
initializer, but use theString.init<T>(describing: T)
initializer if not.
The following standard library types and protocols should be changed to conform to LosslessStringConvertible
.
FloatingPoint
: "FP types should be able to conform. There are algorithms that are guaranteed to turn IEEE floating point values into a decimal representation in a reversible way. I don’t think we care about NaN payloads, but an encoding could be created for them as well." (Chris Lattner)Integer
Bool
: either "true" or "false", since these are their canonical representations.Character
UnicodeScalar
String
String.UTF8View
String.UTF16View
String.CharacterView
String.UnicodeScalarView
StaticString
Once conditional conformance of generic types to protocols is implemented, the additional protocols and types below are candidates for conformance to LosslessStringLiteral
:
RangeReplaceableCollection where Iterator.Element == Character
RangeReplaceableCollection where Iterator.Element == UnicodeScalar
SetAlgebra where Iterator.Element == Character
SetAlgebra where Iterator.Element == UnicodeScalar
ClosedRange where Bound : LosslessStringConvertible
CountableClosedRange where Bound : LosslessStringConvertible
CountableRange where Bound : LosslessStringConvertible
Range where Bound : LosslessStringConvertible
This API change may impact existing code.
Code which intends to invoke init<T>(_: T)
will need to be modified so that the proper initializer is called. In addition, it is possible that this change may uncover instances of the erroneous behavior described previously.
One alternative solution might be to make LosslessStringConvertible
a separate protocol altogether from CustomStringConvertible
and CustomDebugStringConvertible
. Arguments for and against that approach can be found in this earlier version of this proposal.