[swift-evolution] Fwd: [Pitch] Single-quotes for Character literals

Andrew Duncan andrewzboard at gmail.com
Mon Dec 21 17:05:59 CST 2015


I am re-posting this with the [Pitch] header. I have had two responses, which I will include and to which I will respond.

We have all watched the evolution of the String class, and the amazing jockeying required to accommodate Unicode combining characters and the like. At this point, the distinction between Characters and Strings and Arrays of Characters (myString.characters) is becoming clearer.

But in the source code, there is ambiguity. What is "a", a character literal or a string of length 1? I suggest reinstating the C convention of using single-quotes for delimiting Characters.

> On 21 Dec, 2015, at 9:25, Stephen Celis <stephen.celis at gmail.com> wrote:
> 
> What are you looking to solve for here? Have you encountered specific bugs because of this?

I am writing a compiler and need to be aware at all times of the distinction. In the playground, I was toying with

"a b c".characters.split{$0 == " "}.map(String.init)  // ["a", "b", "c"]
Array("abc".characters)	                              // ["a", "b", "c"]

(Rant: my System Prefs have “smart” quotes turned off, but Mail insists. Please advise.)

The first expression yields an array of one-character Strings; the second an array of Characters. Hard to distinguish by eye. Important to distinguish by brain.

> You can usually disambiguate by investigating where resolution takes place (using an IDE like Xcode right now makes this a lot easier).

Agreed, but I think this is still a pretty fundamental point. Not even syntactic but lexical.

> On 21 Dec, 2015, at 14:06, David Waite <david at alkaline-solutions.com> wrote:
> 
> In what context would a character literal (like say '🇺🇸') have value?

Not sure what you mean; the word “value” is ambiguous in that sentence. Clarify?





More information about the swift-evolution mailing list