[Pitch] Single-quotes for Character literals


(Andrew Duncan) #1

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.

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 9:25, Stephen Celis <stephen.celis@gmail.com> wrote:

On 21 Dec, 2015, at 14:06, David Waite <david@alkaline-solutions.com> wrote:

In what context would a character literal (like say ':us:') have value?

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


(Tino) #2

I suggest reinstating the C convention of using single-quotes for delimiting Characters.

I rarely use chars, but from a theoretical standpoint, I'm not so happy with the ambiguity as well…
But one suggestion if you want to make a proposal:
Char has a fixed length (well, at least that was the case before unicode ;-), so there is no need for a closing delimiter. Leaving the second ' (or whatever might be used) out would save one out of three keystrokes and no one could try to create characters like
'this isn't allowed here'

Best regards,
Tino


(Jordan Rose) #3

Unicode is incredibly important, as is deciding whether you want to create a Character or UnicodeScalar (or some other type). In practice I don't think either Character or UnicodeScalar is common enough to deserve a special syntax.

Andrew, you said your use case is a compiler? I believe Dmitri and Dave have some ideas for how better to work with text-like structural data (code, markup, JSON, etc), but they're not ready to share yet. Superficially making Character more convenient won't really help with the rest of the issues in this space; a larger model is needed.

Jordan

···

On Dec 21, 2015, at 15:55 , Tino Heth via swift-evolution <swift-evolution@swift.org> wrote:

I suggest reinstating the C convention of using single-quotes for delimiting Characters.

I rarely use chars, but from a theoretical standpoint, I'm not so happy with the ambiguity as well…
But one suggestion if you want to make a proposal:
Char has a fixed length (well, at least that was the case before unicode ;-), so there is no need for a closing delimiter. Leaving the second ' (or whatever might be used) out would save one out of three keystrokes and no one could try to create characters like
'this isn't allowed here'