# 1 + 'a' = 'b'

**URL:** <https://discourse.julialang.org/t/1-a-b/77947>\
**Category:** General Usage\
**Created:** [March 16, 2022, 5:52am UTC](https://discourse.julialang.org/t/1-a-b/77947 "2022-03-16T05:52:33Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 12:53pm UTC](https://discourse.julialang.org/t/1-a-b/77947/22 "2022-03-16T12:53:06Z")

</div>

> [@DNF](#):
>
> This is because `*(::Char, ::Char)` is not multiplication, but concatenation. Concatenation is something that you definitely can do with letters, but doesn’t make sense with pointers.

Yes, I fully agree.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 12:54pm UTC](https://discourse.julialang.org/t/1-a-b/77947/23 "2022-03-16T12:54:41Z")

</div>

> [@DNF](#):
>
> I don’t think the ‘pointer analogy’ is very useful. Every _value_ has an _encoding_ , an `Int` is just a series of bits with a particular interpretation, as is a `Char` , but that doesn’t make them pointers.

You can call it encoding if you like. It does not change the following:

When we do `'a' + 1` we are manipulating the encoding _not_ the letter itself. Because it does not make sense to increment a letter. It does make sense to increment an encoding.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 16, 2022, 1:31pm UTC](https://discourse.julialang.org/t/1-a-b/77947/24 "2022-03-16T13:31:07Z")

</div>

> [@josuagrw](#):
>
> When we do `'a' + 1` we are manipulating the encoding _not_ the letter itself.

In that perspective, you can never manipulate any object in and of itself, only its encoding. Then _everything_ is a ‘pointer’. I don’t see how this is useful.

Anyway, my point was that ‘pointer’ and ‘encoding’ are not the same, and not really useful to conflate.

> [@josuagrw](#):
>
> Because it does not make sense to increment a letter.

Well, ‘b’ comes after ‘a’, so why not?

BTW: I’m not arguing that `'a' + 1` ought to work, I’m not sure what I think about that.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 2:45pm UTC](https://discourse.julialang.org/t/1-a-b/77947/25 "2022-03-16T14:45:04Z")

</div>

> [@DNF](#):
>
> Well, ‘b’ comes after ‘a’, so why not?

Because this logic extends well beyond the alphabet. After _9_ comes _:_

```julia
julia> '9'+1
':': ASCII/Unicode U+003A (category Po: Punctuation, other)

```

This cannot be explained without reference to encoding.

I have to look at the encoding to understand why I get this specific result.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 2:48pm UTC](https://discourse.julialang.org/t/1-a-b/77947/26 "2022-03-16T14:48:54Z")

</div>

> [@DNF](#):
>
> In that perspective, you can never manipulate any object in and of itself, only its encoding.

I disagree. When I do

```julia
julia> "hello" * " world"
"hello world"

```

I am _not_ operating on the level of the encoding of the strings. It’s completely hidden from me. I am purely operating with the values which are encoded, namely content of the two strings.

The implementation details of the encoding do not determine the result here.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 16, 2022, 2:53pm UTC](https://discourse.julialang.org/t/1-a-b/77947/27 "2022-03-16T14:53:06Z")

</div>

If someone wants this to change in Julia 2.0 they should open an issue about it on GitHub.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 16, 2022, 3:07pm UTC](https://discourse.julialang.org/t/1-a-b/77947/28 "2022-03-16T15:07:18Z")

</div>

This is consistent with the lexicographic order of characters, independent of their encoding.

Anyway, as I said, I don’t care too much about whether `Char + Int` should or shouldn’t work, but that characters aren’t pointers, and aren’t _like_ pointers.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 16, 2022, 3:09pm UTC](https://discourse.julialang.org/t/1-a-b/77947/29 "2022-03-16T15:09:25Z")

</div>

> [@josuagrw](#):
>
> The implementation details of the encoding do not determine the result here.

I was addressing your conflation of ‘encoding’ and ‘pointer’. This is getting really far afield.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 16, 2022, 3:20pm UTC](https://discourse.julialang.org/t/1-a-b/77947/30 "2022-03-16T15:20:01Z")

</div>

> [@josuagrw](#):
>
> ```julia
> julia> '9'+1
> ':': ASCII/Unicode U+003A (category Po: Punctuation, other)
> 
> ```
> 
> This cannot be explained without reference to encoding.

Sure it can - a `Char` is not a number, so what did you expect to happen instead? Having `'10'`? The choice of “next character” is arbitrary and `+ 1` (though implicit casting) happens to be the choice of syntax for “give me the next character”. The choice of what that next character is doesn’t have to make any semantic sense, as there is no such thing in general.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [March 16, 2022, 3:24pm UTC](https://discourse.julialang.org/t/1-a-b/77947/31 "2022-03-16T15:24:22Z")

</div>

I think having `'a' + 1` work makes sense. When I type `'a'` (or any `Char`) at the REPL and hit enter, it displays a number, e.g., `U+0061` in

```julia
julia> 'a'
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

```

Once I see that, it makes sense that adding an integer to a `Char` will change that number by the corresponding amount, e.g., `U+0161` is the result of adding `0x100` to `'a'`:

```julia
julia> 'a' + 0x100
'š': Unicode U+0161 (category Ll: Letter, lowercase)

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [March 16, 2022, 3:32pm UTC](https://discourse.julialang.org/t/1-a-b/77947/32 "2022-03-16T15:32:25Z")

</div>

> [@Sukera](#):
>
> The choice of what that next character is doesn’t have to make any semantic sense, as there is no such thing in general.

I think that might be @josuagrw’s point: The fact that `'9' + 1` results in `':'` is because `1` is added to the ASCII/Unicode encoding of `'9'`.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [March 16, 2022, 3:32pm UTC](https://discourse.julialang.org/t/1-a-b/77947/33 "2022-03-16T15:32:36Z")

</div>

> [@kristoffer.carlsson](#):
>
> I think almost everyone agree that implicit conversion of integers to chars and vice versa is a mistake, yes. It is something that was put in a long time ago and just slipped through the cracks and managed to get into julia 1.0.

> [@Sukera](#):
>
> and `+ 1` (though implicit casting) happens to be the choice of syntax for “give me the next character”.

This is nothing to do with conversion between integers and characters. If `'a'+1` were promoting both to an `Int` the result would be an `Int` and if it were promoting to `Char` then it would fail because `+(::Char,::Char)` is not defined.

A `Char` represents a Unicode character. Unicode characters form an **ordered sequence** (defined by the Unicode spec), part of which is `..., 'a', 'b', 'c', ...` and so `'a'+1` gets the next item in that sequence. This is completely analogous to:

- Pointers: these form a sequence `..., Ptr{Cvoid}(3), Ptr{Cvoid}(4), ...` and so `Ptr{Cvoid}(3)+1` gets the next item in the sequence.
- Integers: these form a sequence `..., 5, 6, ...` and so `5+1` gets the next item in the sequence.

These types being an **ordered sequence** has other useful semantics, like ordering (`'a'<'b'`, `Ptr{Cvoid}(3)<Ptr{Cvoid}(4)` and `5-6` are all true) and differencing (`'b'-'a'`, `Ptr{Cvoid}(4)-Ptr{Cvoid}(3)` and `6-5` are all 1).

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 3:34pm UTC](https://discourse.julialang.org/t/1-a-b/77947/34 "2022-03-16T15:34:18Z")

</div>

> [@StevenWhitaker](#):
>
> I think that might be @josuagrw’s point: The fact that `'9' + 1` results in `':'` is because `1` is added to the ASCII/Unicode encoding of `'9'` .

That is exactly my point, thank you for translating.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [March 16, 2022, 3:42pm UTC](https://discourse.julialang.org/t/1-a-b/77947/35 "2022-03-16T15:42:31Z")

</div>

For the mathematically inclined, `+(::Char,::Int)` forms a [group action](https://www.wikiwand.com/en/Group_action) of the (additive) group of integers on the set of characters.

Similarly `+(::Ptr{T},::Int)` is a group action of integers on pointers and `+(::Int,::Int)` is the usual group action of integers on themselves, namely the (additive) group operation on integers. So this is all consistent and legit from a mathematical point of view.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 16, 2022, 3:46pm UTC](https://discourse.julialang.org/t/1-a-b/77947/36 "2022-03-16T15:46:42Z")

</div>

> [@StevenWhitaker](#):
>
> I think that might be @josuagrw’s point: The fact that `'9' + 1` results in `':'` is because `1` is added to the ASCII/Unicode encoding of `'9'` .

> [@cjdoris](#):
>
> This is nothing to do with conversion between integers and characters.

> [@cjdoris](#):
>
> A `Char` represents a Unicode character. Unicode characters form an **ordered sequence**

Yes, I’m aware of that. My point is that this has nothing to do with the _encoding_, as that argument breaks down as soon as you have multibyte characters (same goes for the pointer argument!). That’s why I’ve been very careful to say “next character” instead (which noone has mentioned so far, as far as I can tell).

Viewed from that perspective, one could also argue that `iterate(Char, 1234)` should give the 1234th character in UTF-8, though I think hardly anyone would think that as a sensible API either.

The point made by @kristoffer.carlsson and @StefanKarpinski above still stands - this is the API for “next character” we currently have and we can’t get rid of it until 2.0. The problem is known and issues for it exist, so arguing about it being horrible/bad for students/whatever else is futile, as we have to live with it for now.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 16, 2022, 3:46pm UTC](https://discourse.julialang.org/t/1-a-b/77947/37 "2022-03-16T15:46:56Z")

</div>

There’s also subtraction defined between characters to give you the integer difference in code points. This can be useful, if cutesy:

```julia
julia> rot(str, n) = String((collect(str) .- 'a' .+ n) .% 26 .+ 'a')
rot (generic function with 1 method)

julia> rot("hello", 13)
"uryyb"

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 16, 2022, 3:47pm UTC](https://discourse.julialang.org/t/1-a-b/77947/38 "2022-03-16T15:47:04Z")

</div>

> [@cjdoris](#):
>
> from a mathematical point of view.

which doesn’t really help for the original question.  
But it generates more questions, like: should we than treat Float64 as sequence in this mathematical sense and distinguish between `1.0 + 1` and `1.0 + 1.0`. I don’t think so.

The question is about implicit casts, are they error prone or can they be tolerated in special circumstances.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [March 16, 2022, 3:48pm UTC](https://discourse.julialang.org/t/1-a-b/77947/39 "2022-03-16T15:48:42Z")

</div>

> [@Sukera](#):
>
> that argument breaks down as soon as you have multibyte characters

What happens when you add 1 to a multibyte character?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 16, 2022, 3:49pm UTC](https://discourse.julialang.org/t/1-a-b/77947/40 "2022-03-16T15:49:50Z")

</div>

> [@oheil](#):
>
> The question is about implicit casts, are they error prone or can they be tolerated in special circumstances.

This isn’t an implicit cast, nor is it promotion (like your numeric `1` vs `1.0` example). It’s just that we’ve chosen a meaning for what happens when you add an integer and a char together.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 16, 2022, 3:51pm UTC](https://discourse.julialang.org/t/1-a-b/77947/41 "2022-03-16T15:51:11Z")

</div>

It is implicit.  
Admitted not a cast, but still implicit. And not explicitly written by the user/programmer as in `Char(Int('a')+1)`.

[Previous page](https://discourse.julialang.org/t/1-a-b/77947.md?page=1)

[Next page](https://discourse.julialang.org/t/1-a-b/77947.md?page=3)
