Isn’t this one of those things they tell us never to use? aiming a gun at your foot shouldn’t be the price you have to pay for an ASCII literal
Counterpoint: beginner exercises tend to involve processing fixed-length, structured, machine-readable strings. Think of any old CS homework problem—does it involve UTF8 continuations or grapheme breaking? If you want to teach a beginner how to get the query out of a URL string, you don’t want to throw Unicode at them, you just want them to treat/convert the String
as an array of ASCII characters. (ASCII characters are actually the only characters valid in a url). There’s been a lot of lament on the forum about how Swift String
s and its unicode-correct indexing model are getting in the way of Swift education and adoption, and I’ve always considered better ASCII support to be the most appropriate solution to this problem.
Unicode is an advanced topic for advanced users, and Character
s are Unicode, a lot more than Unicode.Scalar
, ironically. Maybe placing this API on the higher shelf isn’t the worst idea in the world.
I agree, a trapping property is not appropriate on either Character
or Unicode.Scalar
. There would be no language precedent,, .first
and .last
are important, commonly used properties, and we do not make them trap.
I don’t know how I feel about abusing function call syntax (yes, 'a'.ascii
is a function call, just written without parentheses) for literal coercion. Why not the 'A' as UInt8
syntax? I think as
is a natural fit for this role, given its current usage in the language, and the literal | as
grammar is well-established in Swift, so we wouldn’t have to teach users a new rule “‘literal bound functions’ can only be called on literal values”.