swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.64k stars 10.38k forks source link

[SR-9032] Add ability to create ‘character' literals to compiler. #51535

Open johnno1962 opened 6 years ago

johnno1962 commented 6 years ago
Previous ID SR-9032
Radar None
Original Reporter @johnno1962
Type New Feature
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | Compiler | |Labels | New Feature, LanguageFeatureRequest | |Assignee | @johnno1962 | |Priority | Medium | md5: 82dd26a9f4af318193363fdfc65d9a9d

Issue Description:

It is sometimes useful to be able to express the ascii value of a character more conveniently than the existing UInt8(ascii: “a”) and now that the syntax for raw strings has taken a different direction it is suggested this be simply ‘a’ as it is in many other languages. These would be Character literals in the Swift sense, able to express any UNICODE extended grapheme cluster but if the cluster contains only a single code point it should be possible to express Swift’s integer types using this literal syntax. As examples:

let a: Int8 = ‘a’
let newline: Int8 = ‘\n’

belkadan commented 6 years ago

I know there's a long thread about this but I feel it's worth pointing out that we have this: it's UnicodeScalar.

let x: UnicodeScalar = "x"

The proposal here would be extending that to work for plain integers if the value is known to fit in the integer, I guess.