Is this expected behavior? Should it even be possible to instantiate a
let myChar = Character.init(" a👩👩👧👦bc") print(myChar) // a👩👩👧👦bc print(myChar.isWhitespace) // true print(myChar.isLetter) // false print(myChar.isLowercase) // true
myChar is of type
Character, not eg
Which .init is being called there?
Alt-clicking the init in Xcode suggests that it is
init?(_ description: String) but that can't be right since the following shows that it isn't a failable initializer:
let myChar2 = Character.init(" a👩👩👧👦bc")! // ERROR: Cannot force unwrap value of non-optional type `Character`
Jumping to the definition of the intit will take me to this (declared in an extension to Character):
/// Creates a character from a single-character string. /// /// The following example creates a new character from the uppercase version /// of a string that only holds one character. /// /// let a = "a" /// let capitalA = Character(a.uppercased()) /// /// - Parameter s: The single-character string to convert to a `Character` /// instance. `s` must contain exactly one extended grapheme cluster. @inlinable public init(_ s: String)
which I guess might be true. But the documentation of this initializer says that the given string must be a single-character string that contains exactly one extended grapheme cluster (which the string of my example doesn't), and it says nothing about what should happen if the given string doesn't meet those requirements. Shouldn't it trap or be a failable initializer?
Is the Character
myChar really a "single extended grapheme cluster that approximates a user-perceived character"?
And if so, what does that user-perceived character look like? The following does not work (as expected):
let myChar3: Character = " a👩👩👧👦bc" // ERROR: Cannot convert value of type 'String' to specified type 'Character'
while eg the following does (as expected):
let myChar4: Character = "👩👩👧👦" // Compiles