Hi folks,
The review of SE-0405: String Initializers with Encoding Validation ended on August 29. The Language Steering Group as decided to accept the proposal with modifications to the validatingAsUTF8:
overloads.
Review feedback was light overall but in favor of adding initializers for string validation. The Language Steering Group believes that init?(validating:as:)
taking a Sequence
of code units is the right shape for this API in general.
More discussion centered around the two proposed init?(validatingAsUTF8:)
overloads, with some reviewers asking whether they were necessary given the proposed more general API or whether the choice of parameter type was the most useful. The proposal argued that becuase UTF-8 is "the most common case", a UTF-8-specific overload would be convenient and discoverable.
The Language Steering Group discussed these overloads and we feel that convenience and discoverability alone are not enough of a motivating factor to add separate, less general overloads. The key question we asked ourselves was, "if the tooling were improved to make UTF-8 usage of the general API more discoverable, would we still want the less general API?" and we agreed that the answer to this question as that we would not. Therefore, we believe that this is an area that tooling can and should improve. For APIs like init?(validating:as:)
in particular that take a small set of protocol-conforming metatypes, we would like to see autocomplete have better support for filling in the possible choices as the user writes code.
Regarding the other proposed init?(validatingAsUTF8:)
overload intended mainly for C interop, the Language Steering Group believes that an overload init?(validating: some Sequence<Int8>, as: UTF8.self)
aligns nicely with the general API above and this pair of APIs portably supports data obtained from C without concern for the underlying type signedness.
The Language Steering Group also agrees—even though the removal of the init?(validatingAsUTF8:)
overloads eliminates the name near-collision with the existing init?(validatingUTF8:)
initializer—that the latter initializer is still poorly named and that it should be deprecated and replaced by init?(validatingCString:)
.
In summary, we are accepting the following set of APIs:
extension String {
public init?<Encoding: Unicode.Encoding>(
validating codeUnits: some Sequence<Encoding.CodeUnit>,
as: Encoding.Type)
public init?(
validating codeUnits: some Sequence<Int8>,
as: UTF8.Type)
public init?(
validatingCString nullTerminatedUTF8: UnsafePointer<CChar>)
@available(Swift 5.XLIX, deprecated, renamed:"String.init(validatingCString:)")
public init?(validatingUTF8 cString: UnsafePointer<CChar>)
}
Thank you to everyone who participated in the pitch and proposal review! Your contributions help make Swift a better language.
—Tony Allevato
Review Manager