# Prepitch: Character integer literals

**URL:** <https://forums.swift.org/t/prepitch-character-integer-literals/10442>\
**Category:** Pitches\
**Created:** [February 28, 2018, 11:08pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442 "2018-02-28T23:08:40Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 1, 2018, 6:16am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/21 "2018-03-01T06:16:12Z")

</div>

> [@allevato](#):
>
> > [@Chris\_Lattner3](#):
> >
> > This can be made to work, but the logical endpoint of this is that UInt16 would allow code points that fit into a UTF16 size and UInt32 would allow any code point. Is that desirable?
> 
> I think the argument could be made that we want to provide APIs that satisfy the most common needs of users without worrying about taking things to their logical end. Unscientifically, my gut tells me that processing ASCII text that arrives in the form of `[UInt8]` or `Data` probably qualifies as more valuable to have as shorthand than UTF-16 or UTF-32.

i hear legends of some (rare, old-school) platforms and contexts where ascii data is stored in 16 or even 32 bit integers, with the high bits zeroed out or ignored. but i agree that `UInt8` is the main one to support.

> [@allevato](#):
>
> Another option would be to introduce a unique `ASCIICodeUnit` type that is essentially a 7-bit unsigned integer instead of extending `UInt8` directly, but that might make interop with `UInt8`s more verbose elsewhere.

i see no real benefit to this, ascii is usually stored in 8-bits so you basically get the other 128 character plane for free. maybe we should use signed `Int8` so you can use a sign test to distinguish the two

> [@allevato](#):
>
> By itself, a new quoting scheme specifically for `Character` feels like a separate issue than the one in the OP about making it cleaner to write ASCII literals that can be inferred as `UInt8`. Unless, are you suggesting that the scheme could be stretched such single quotes would default to `Character` but also be inferred as other types representing "singular" text entities (ASCII `UInt8`, `Unicode.Scalar`) and double quotes would only be inferred as sequences of those entities (`String`, `StaticString`)?

i feel like y’all getting hung up on the single quotes vs double quotes thing which is really its own side issue and not particularly relevant here. again, i suggest single quotes because i think `"` is way too overloaded in the language and people are too stingy with reserving `'` for something else but there’s no real problem with using `"` for everything

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 6:16am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/22 "2018-03-01T06:16:29Z")

</div>

> [@allevato](#):
>
> By itself, a new quoting scheme specifically for Character feels like a separate issue than the one in the OP about making it cleaner to write ASCII literals that can be inferred as UInt8. Unless, are you suggesting that the scheme could be stretched such single quotes would default to Character but also be inferred as other types representing “singular” text entities (ASCII UInt8, Unicode.Scalar) and double quotes would only be inferred as sequences of those entities (String, StaticString)?

What I'm getting at is that this can be solved in multiple ways: 1) introducing new types, 2) introducing a new literal form, 3) allowing the existing string literal forms to work with existing integer types.

#1 is bad because it doesn't work with existing APIs and common cases, it leads to syntactic bloat for the common cases we're trying to sugar. #3 leads to potential for confusion and "creep" problems as I mentioned upthread.

I don't see the downside of #2. We didn't use single quotes initially because we thought that they could be used for multi-line string literals, but that has already been resolved. I don't see the downside to taking the (highly precedented! :-) approach of using them for character literals. Once you do that, then the behavior is immediately obvious:

```swift
  let a = 'c' // Character
  let b : Int8 = 'x' // ok
  let c : Int16 = '˚' // ok if it fits.
  let d : Int = '\u{123}' // ok

  let e : Int8 = "x" // obviously not ok, "x" is a string.

```

etc. What's the bad thing about this approach? What possibility for confusion or other problem do you forsee?

-Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 6:19am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/23 "2018-03-01T06:19:54Z")

</div>

Going back to the OP, this C code:

> [@taylorswift](#):
>
> static char const hexcodes[16] = {  
> '0', '1', '2', '3', '4' ,'5', '6', '7', '8', '9',  
> 'a', 'b', 'c', 'd', 'e', 'f'  
> };

would be expressible like this:

```swift
let hexcodes: [UInt8] = 
    ['0', '1', '2', '3', '4' ,'5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

```

which seems to directly address the problem at hand.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 1, 2018, 6:20am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/24 "2018-03-01T06:20:37Z")

</div>

right now `Unicode.Scalar` is really bad to use just because it uses `"` which means you have to type `as Unicode.Scalar` everywhere which is annoying. making use of `'` could make things better, and it’s not like anyone else has any serious plans for this character anytime soon

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 1, 2018, 6:35am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/25 "2018-03-01T06:35:48Z")

</div>

> [@Chris\_Lattner3](#):
>
> What’s the bad thing about this approach? What possibility for confusion or other problem do you forsee?

I can't say that I'm against it, especially now that it's been fleshed out a bit more with the other types you gave examples of. 🙂

My initial hesitation was to the `Character` type specifically being treated differently from other strings or scalars, but I think if we partitioned the various types by their cardinality and said:

- Single quotes represent literals that can be treated as a **single** ASCII code unit, UTF-16 code unit, `Unicode.Scalar`, (_edit:_ or `Character`), depending on the type it's being coerced to
- Double quotes represent literals that contain may only be coerced to types that support **multiple** characters

...then we'd be telling a consistent and compelling story.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 6:40am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/26 "2018-03-01T06:40:02Z")

</div>

+1 with the extension that single quotes should allow a single grapheme cluster as well, e.g. when of type Character.

I believe that this can all be added in an additive and compatible manner. We would want to eventually deprecate these though:

```swift
   Int8(ascii: "x") // should migrate to single quotes, and possibly eliminate the initializer outright.
   let x : Character = "y" // should migrate to using single quotes instead of double.

```

I don't see a rush to doing this though.

-Chris

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 1, 2018, 6:43am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/27 "2018-03-01T06:43:33Z")

</div>

> [@Chris\_Lattner3](#):
>
> +1 with the extension that single quotes should allow a single grapheme cluster as well, e.g. when of type Character.

Indeed, I didn't mean to leave that one out! (Edited above.)

> [@Chris\_Lattner3](#):
>
> We would want to eventually deprecate these though:

Also this one, correct?

```swift
let x: Unicode.Scalar = "y" // should migrate to single quotes

```

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 6:46am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/28 "2018-03-01T06:46:03Z")

</div>

Yep, everything like that.

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [March 1, 2018, 10:08am UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/29 "2018-03-01T10:08:12Z")

</div>

Okay, if it's used consistently with `Character` etc, cleanly separating a single character or UTF-8/UTF-16 code point from multiple, then I can see the argument for it. It's not that compelling to me personally, and I feel like they would be more useful as e.g. the raw string feature that has been discussed here previously, but it does provide a clean syntax for people who work with characters-as-integers a lot. I don't know how large that audience is, though.

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [March 1, 2018, 4:46pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/30 "2018-03-01T16:46:24Z")

</div>

> [@Chris\_Lattner3](#):
>
> This can be made to work, but the logical endpoint of this is that UInt16 would allow code points that fit into a UTF16 size and UInt32 would allow any code point. Is that desirable?

In [swift-corelibs-foundation](https://github.com/apple/swift-corelibs-foundation/blob/b41506ce7918f4b5eff7374045dc319c896db8af/Foundation/NSString.swift#L13-L21), `UInt16` is already `ExpressibleByUnicodeScalarLiteral`:

```swift
public typealias unichar = UInt16

extension unichar : ExpressibleByUnicodeScalarLiteral {
    public typealias UnicodeScalarLiteralType = UnicodeScalar
    
    public init(unicodeScalarLiteral scalar: UnicodeScalar) {
        self.init(scalar.value)
    }
}

```

@Tony_Parker, should this be removed for compatibility with Darwin platforms?

---

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [March 1, 2018, 5:13pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/31 "2018-03-01T17:13:04Z")

</div>

> [@Chris\_Lattner3](#):
>
> +1 with the extension that single quotes should allow a single grapheme cluster as well, e.g. when of type Character.

There was an issue with how to validate `Character` literals:

[[SR-4546] Need more permissive ExtendedGraphemeClusterLiteral parsing](https://bugs.swift.org/browse/SR-4546)

I don't know if this will be re-evaluated for Swift 5.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 1, 2018, 9:28pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/32 "2018-03-01T21:28:49Z")

</div>

just to be clear, what is the integer value of a single-quoted `Character` containing multiple `Unicode.Scalar`s?

---

<div class="post-metadata">

**Author:** ![tali](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tali/32/11346_2.png) [@tali](https://forums.swift.org/u/tali)\
**Post date:** [March 1, 2018, 9:33pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/33 "2018-03-01T21:33:06Z")

</div>

> just to be clear, what is the integer value of a single-quoted `Character` containing multiple `Unicode.Scalars`?

That should be a compile error.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 1, 2018, 10:41pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/34 "2018-03-01T22:41:26Z")

</div>

Yep. I would expect the following behavior:

```swift
let x1: Character = 'a' // ok
let x2: Int = 'a' // ok, x2 == 97
let x3: Character = '🇨🇦' // ok
let x4: Int = '🇨🇦' // compile error, not a single scalar

```

This is the same behavior we already have today if you try to initialize a `Unicode.Scalar` from a double-quoted string literal with more than one scalar, or a `Character` from a double-quoted string literal with multiple grapheme clusters.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 10:41pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/35 "2018-03-01T22:41:57Z")

</div>

> [@taylorswift](#):
>
> just to be clear, what is the integer value of a single-quoted Character containing multiple Unicode.Scalars?

I'm not sure what your question is. A single grapheme cluster (which is what Character models) is a single character that may be composed of multiple unicode scalars. It is all of the unicode scalars that make up that character, not a single one.

-Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 1, 2018, 10:43pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/36 "2018-03-01T22:43:01Z")

</div>

+1 to @allevato's post. Exactly.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 1, 2018, 10:51pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/37 "2018-03-01T22:51:47Z")

</div>

that’s exactly the point i’m making, now all of a sudden we have `''` literals that fail depending on the inferred type

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 1, 2018, 10:59pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/38 "2018-03-01T22:59:37Z")

</div>

> [@taylorswift](#):
>
> that’s exactly the point i’m making, now all of a sudden we have '' literals that fail depending on the inferred type

Why is this a problem? Your original post was asking for a way to cleanly get the integer value of an ASCII code unit, and this does that, with the added benefit that it supports more than just ASCII and you get compile-time verification that the value can be represented by the inferred type.

I don't understand how that objection would be resolved unless you say that single quotes could only support the lowest common denominator, which would be 7-bit ASCII, and I think that would be far too restrictive to exhaust single quotes for.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 1, 2018, 10:59pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/39 "2018-03-01T22:59:43Z")

</div>

can I suggest that instead `Character` still use double quotes because `Character`s are really sequences of scalar code units instead of scalars themselves. we kind of have this weird hierarchy in our string model where there’s `Unicode.Scalar` ∈ `Character` ⊆ `String`.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 1, 2018, 11:17pm UTC](https://forums.swift.org/t/prepitch-character-integer-literals/10442/40 "2018-03-01T23:17:06Z")

</div>

While that seems like a reasonable _technical_ model, it would provide an inconsistent user experience if we look at the whole picture. Let's consider a double-quoted string literal `"abc"` and how we would view its elements under various string views under that model:

- The UTF-8 code units of `"abc"` are equivalent to `['a', 'b', 'c']`
- The UTF-16 code units of `"abc"` are equivalent to `['a', 'b', 'c']`
- The scalars of `"abc"` are equivalent to `['a', 'b', 'c']`
- The characters of `"abc"` are equivalent to `["a", "b", "c"]`

Keeping `Character` literals as double-quoted breaks what is otherwise a clean and consistent mapping between string elements and sequences of string elements. I can see both sides of this, but I have to say I prefer the solution that makes all four bullets above consistent because that's an easier relationship to explain to newcomers to Swift.

[Previous page](https://forums.swift.org/t/prepitch-character-integer-literals/10442.md?page=1)

[Next page](https://forums.swift.org/t/prepitch-character-integer-literals/10442.md?page=3)
