Prepitch: Character integer literals

I'd like to propose a radically different approach — inspired from the concerns about non-ASCII characters:

  • double quotes mean Unicode string, character, or scalar
  • single quotes mean ASCII string or scalar

So whenever you need to be sure something is purely ASCII you use single-quotes:

let a: String = "planté" // unicode string
let b: String = 'planté' // ERROR: é is not ascii
let c: String = 'plante' // ascii string (no é in this one)
let a: Character = "é" // U+00E9
let b: Character = 'é' // ERROR: U+00E9 is not ascii
let c: Character = 'p' // U+0070
let a: UnicodeScalar = "é" // U+00E9
let b: UnicodeScalar = 'é' // ERROR: U+00E9 is not ascii
let c: UnicodeScalar = 'p' // U+0070

We can then allow ASCII literals to initialize any numeric type:

let a: UInt32 = "é" // ERROR: UInt32 does not conform to ExpressibleByUnicodeScalarLiteral
let b: UInt32 = 'é' // ERROR: U+00E9 is not ascii
let c: UInt32 = 'p' // 0x00000070
let a: UInt16 = "é" // error: UInt16 does not conform to ExpressibleByUnicodeScalarLiteral
let b: UInt16 = 'é' // error: U+00E9 is not ascii
let c: UInt16 = 'p' // 0x0070
let a: UInt8 = "é" // ERROR: UInt8 does not conform to ExpressibleByUnicodeScalarLiteral
let b: UInt8 = 'é' // ERROR: U+00E9 is not ascii
let c: UInt8 = 'p' // 0x70

And you can also initialize an array of numbers from an ASCII string:

let a: [UInt8] = "plante" // ERROR: Array does not conform to ExpressibleByStringLiteral
let b: [UInt8] = 'plante' // ascii
let a: [UInt16] = "plante" // ERROR: Array does not conform to ExpressibleByStringLiteral
let b: [UInt16] = 'plante' // ascii

Of course, this approach completely flips on its head the current character literal proposal.

10 Likes