- Proposal: SE-0128
- Author: Xin Tong
- Review Manager: Chris Lattner
- Status: Implemented (Swift 3.0)
- Decision Notes: Rationale
- Implementation: apple/swift#3662
This proposal aims to change some UnicodeScalar
initializers (ones that are non-failable)
from non-failable to failable. i.e., in case a UnicodeScalar
can
not be constructed, nil is returned.
Currently, when one passes an invalid value to the non-failable UnicodeScalar
UInt32
initializer, it crashes the program by calling _precondition
.
This is undesirable if one wishes to initialize a Unicode scalar from unknown input.
Mark the non-failable UnicodeScalar
initializers as failable and return nil when the integer is not a valid Unicode codepoint.
Currently, the stdlib crashes the program by calling
_precondition
if the integer is not a valid Unicode codepoint. For example:
var string = ""
let codepoint: UInt32 = 55357 // this is invalid
let ucode = UnicodeScalar(codepoint) // Program crashes at this point.
string.append(ucode)
After marking the initializer as failable, users can write code like this and the program will execute fine even if the codepoint isn't valid.
var string = ""
let codepoint: UInt32 = 55357 // this is invalid
if let ucode = UnicodeScalar(codepoint) {
string.append(ucode)
} else {
// do something else
}
The initializers are now failable, returning an optional, so optional unwrapping is necessary.
The API changes include:
public struct UnicodeScalar {
- public init(_ value: UInt32)
+ public init?(_ value: UInt32)
- public init(_ value: UInt16)
+ public init?(_ value: UInt16)
- public init(_ value: Int)
+ public init?(_ value: Int)
}
Leave status quo and force the users to do input checks before trying to initialize
a UnicodeScalar
.