But I don’t think I do agree. If they were single quotes yes but I don’t recommend making potentially source breaking changes to things that String’s conform to via ExpressibleByUnicodeScalar
. Cleaner to give single-quoted, single-codepoint literals their own new protocol ExpressibleByCodepointLiteral
and conform Int & co. to that.
In the case where the literal is not a single codepoint I’ve taken out the error and had the parser re-lex the token as a String in the prototype. The means single quoted characters can be either integer (modified to look for conformance to ExpressibleByCodepointLiteral
) or string literals. This probably sounds a little on the pragmatic side but isn’t that different from container literals which can be Arrays or Dictionaries depending on their first element. In practical terms this means the following is now possible:
let x3: Character = ‘🇨🇦' // ok (yay!)
But you get some gnarly errors when you mix the two (default type is now Unicode.Scalar):
k.swift:6:20: error: binary operator '-' cannot be applied to operands of type 'Unicode.Scalar' and 'String'
let d2: Int8 = '*' - '👨🏼🚀'
k.swift:6:20: note: overloads for '-' exist with these partially matching parameter lists: (Float, Float), (Double, Double), (Float80, Float80), (UInt8, UInt8), (Int8, Int8), (UInt16, UInt16), (Int16, Int16), (UInt32, UInt32), (Int32, Int32), (UInt64, UInt64), (Int64, Int64), (UInt, UInt), (Int, Int), (Self, Self), (Self, Self.Stride)
I think it was better to catch the error early on myself. The following is still illegal:
k.swift:2:21: error: cannot convert value of type 'String' to specified type 'Character'
let a1: Character = '🇨🇦🇨🇦’