# 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:** 1

<div class="post-metadata">

**Author:** ![Jerry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry/32/34537_2.png) [@Jerry](https://discourse.julialang.org/u/Jerry)\
**Post date:** [March 16, 2022, 5:52am UTC](https://discourse.julialang.org/t/1-a-b/77947/1 "2022-03-16T05:52:33Z")

</div>

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

```

This is ridiculous. How is this not an error? There should be—and maybe there is—an increment function for characters. I know this aggressive behind-the-scenes overloading is a much-loved feature of Julia but to me it is the complete opposite of type safety. Sure, I can type-safe my own functions—until I forget. Sorry, not trying to be flamey, just my 2c.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 16, 2022, 5:58am UTC](https://discourse.julialang.org/t/1-a-b/77947/2 "2022-03-16T05:58:08Z")

</div>

This might very well be a legacy from languages where `char` is a type of integer, usually the smallest non-boolean integer being a single byte, and people are used to being able to do arithmetic on chars for that reason. Some languages also have unsigned chars similar to other unsigned integers. You can also cast a char to its integer value using

```julia
julia> Int('a')
97

```

but interestingly you cannot compare the two

```julia
julia> 'a' > 90
ERROR: MethodError: no method matching isless(::Int64, ::Char)

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 16, 2022, 6:43am UTC](https://discourse.julialang.org/t/1-a-b/77947/3 "2022-03-16T06:43:32Z")

</div>

This is [documented behavior](https://docs.julialang.org/en/v1/manual/strings/#man-characters) which is explicitly implemented [here](https://github.com/JuliaLang/julia/blob/ac1d69302ce8e682bef3108296f5a6c182b62e5f/base/char.jl#L237-L247). You can, of course, commit type piracy, but this may break many things in awful ways.

```julia
julia> import Base: +

julia> +(a::Char, b::Int) = error("ridiculous!")
+ (generic function with 209 methods)

julia> 1 + 'a'
ERROR: ridiculous!
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] +(a::Char, b::Int64)
   @ Main .\REPL[2]:1
 [3] +(x::Int64, y::Char)
   @ Base .\char.jl:247
 [4] top-level scope
   @ REPL[3]:1

julia> 'b' in 'a':'z'
ERROR: ridiculous!
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] +(a::Char, b::Int64)
   @ Main .\REPL[2]:1
 [3] _colon(start::Char, step::Int64, stop::Char)
   @ Base .\range.jl:45
 [4] (::Colon)(start::Char, step::Int64, stop::Char)
   @ Base .\range.jl:40
 [5] (::Colon)(start::Char, stop::Char)
   @ Base .\range.jl:7
 [6] top-level scope
   @ REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 16, 2022, 7:12am UTC](https://discourse.julialang.org/t/1-a-b/77947/4 "2022-03-16T07:12:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [March 16, 2022, 10:06am UTC](https://discourse.julialang.org/t/1-a-b/77947/5 "2022-03-16T10:06:42Z")

</div>

> [@kristoffer.carlsson](#):
>
> I think almost everyone agree that implicit conversion of integers to chars and vice versa is a mistake, yes.

If this is the case, does the issue exist already, or should someone write an issue?

---

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [March 16, 2022, 10:14am UTC](https://discourse.julialang.org/t/1-a-b/77947/6 "2022-03-16T10:14:25Z")

</div>

I like being able to do `'a'+1`. It may be a sharp tool, but I appreciate it.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 16, 2022, 10:37am UTC](https://discourse.julialang.org/t/1-a-b/77947/7 "2022-03-16T10:37:14Z")

</div>

> [@Tero\_Frondelius](#):
>
> If this is the case, does the issue exist already, or should someone write an issue?

Yes, [Unexpected behavior due to implicit `convert` with `Char` and `Integer` · Issue #44410 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/44410).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 16, 2022, 10:37am UTC](https://discourse.julialang.org/t/1-a-b/77947/8 "2022-03-16T10:37:55Z")

</div>

> [@jonas-schulze](#):
>
> I like being able to do `'a'+1` . It may be a sharp tool, but I appreciate it.

You would just need to write `'a' + Char(1)` instead.

---

<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, 10:55am UTC](https://discourse.julialang.org/t/1-a-b/77947/9 "2022-03-16T10:55:18Z")

</div>

How would that work? Adding `Char` to `Char` seems like a strange operation, moreso than adding an `Int` to a `Char`.

Seems like it should be

```julia
Char(Int('a') + 1)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 16, 2022, 10:56am UTC](https://discourse.julialang.org/t/1-a-b/77947/10 "2022-03-16T10:56:28Z")

</div>

Yeah, that’s right.

---

<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, 11:36am UTC](https://discourse.julialang.org/t/1-a-b/77947/12 "2022-03-16T11:36:32Z")

</div>

I think the confusion comes from what `'a'` is. Many here implicitly assume that `'a'` is a representation of the letter _a_. However, in Julia that would be `:a` (or `"a"`).

Instead `'a'` is actually the code point for the letter _a_ in ASCII. It is essentially a pointer. And for a pointer, it makes sense to have integer addition defined.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [March 16, 2022, 11:45am UTC](https://discourse.julialang.org/t/1-a-b/77947/13 "2022-03-16T11:45:12Z")

</div>

I disagree. `'a'` is most definitely a representation of the letter `a`. If we wanted to just talk about Unicode codepoints, we would be using `UInt32`, since that is actually a number.

In my opinion, you are confusing the _representation_ of a type with its _semantics_. Dig into a `DateTime` and you’ll find it’s composed of a single `Int`. Does that mean a point in time is really an integer? No. It really is a time point. The implementation is incidental.

---

<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, 11:57am UTC](https://discourse.julialang.org/t/1-a-b/77947/14 "2022-03-16T11:57:52Z")

</div>

The behaviors tell us what the thing really is. The way `'a'` behaves w.r.t. integer addition tells me that the people who implemented it were thinking about it as a pointer.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [March 16, 2022, 11:59am UTC](https://discourse.julialang.org/t/1-a-b/77947/15 "2022-03-16T11:59:00Z")

</div>

Indeed it originally was an integer (not a pointer, it has nothing to do with memory). But it was decided to make it not an integer. Apparently some methods stuck around so now it’s a weird mix between a character and an integer, which is exactly the substance of the gripe of this thread.

---

<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:03pm UTC](https://discourse.julialang.org/t/1-a-b/77947/16 "2022-03-16T12:03:56Z")

</div>

Do you have examples of letter-like behaviors for `'a'`?

---

<div class="post-metadata">

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

</div>

```julia
julia> print('a') # prints like a letter
a
julia> isuppercase('a') # has text-like methods
false

julia> uppercase('a')
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)

```

Edit: And more literally:

```julia
julia> isnumeric('a')
false

julia> isletter('a')
true

```

---

<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:08pm UTC](https://discourse.julialang.org/t/1-a-b/77947/18 "2022-03-16T12:08:06Z")

</div>

To be honest I don’t find these very distressing. They all fit with the notion of `'a'` being a pointer to something (compare for example `Vector` which is also a pointer to some memory but prints the content of that memory under `print`).

To me this is really something I would not expect from a pointer:

```julia
julia> 'a' * 'b'
"ab"

```

EDIT: The `isletter` is weird.

---

<div class="post-metadata">

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

</div>

I think I might be confused with what you mean by ‘a’ being a “pointer”. `Char` is certainly not implemented using an actual pointer or a reference, and is pass-by-value.

---

<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:14pm UTC](https://discourse.julialang.org/t/1-a-b/77947/20 "2022-03-16T12:14:41Z")

</div>

I mean “pointer” not in the sense of “pointing to some page in memory” but in the sense of “pointing to a letter in the ASCII/Unicode table”.

What are we doing with `'a' + 1`? We are moving to the next entry in the ASCII/Unicode table. We are incrementing the pointer.

---

<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, 12:38pm UTC](https://discourse.julialang.org/t/1-a-b/77947/21 "2022-03-16T12:38:35Z")

</div>

> [@josuagrw](#):
>
> To me this is really something I would not expect from a pointer:
> 
> ```julia
> julia> 'a' * 'b'
> "ab"
> 
> ```

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.

> [@josuagrw](#):
>
> What are we doing with `'a' + 1` ? We are moving to the next entry in the ASCII/Unicode table. We are incrementing the pointer.

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.

I think this is just confusing the discussion.

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