# Append! to an Int array with string (bug?)

**URL:** https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157
**Category:** New to Julia
**Created:** [November 18, 2017, 7:58pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157 "2017-11-18T19:58:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)
#### Post date: [November 18, 2017, 7:58pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/1 "2017-11-18T19:58:40Z")

</div>

I was messing around in the REPL with `append!` and noticed the following behavior:

```julia
julia> a1 = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> append!(a1, "AAAA")
7-element Array{Int64,1}:
  1
  2
  3
 65
 65
 65
 65

```

While I understand what is going on here (the string “AAAA” is being treated as an array of `Char` like in C), is this the expected behavior for Julia? I would’ve expected to either get an error because of incompatible types or promotion of the `a1` array to `Any`, but I wasn’t expecting that the string would be treated as an array of chars.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [November 18, 2017, 8:12pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/2 "2017-11-18T20:12:27Z")

</div>

> [@randyzwitch](#):
>
> append!(a1, “AAAA”)

```julia
julia> @edit append!(a1, "AAAA")

```

shows that it is calling the `append!` method for an iterable, and indeed iterating through a `String` does the following:

```julia
julia> for i in "AAAA"
       @show i, typeof(i)
       end
(i, typeof(i)) = ('A', Char)
(i, typeof(i)) = ('A', Char)
(i, typeof(i)) = ('A', Char)
(i, typeof(i)) = ('A', Char)

```

---

<div class="post-metadata">

### Author: ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)
#### Post date: [November 20, 2017, 3:49pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/3 "2017-11-20T15:49:23Z")

</div>

Thanks for confirming the behavior. My real question is whether it should behave this way? Do we treat strings as Arrays of Chars elsewhere?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [November 20, 2017, 3:55pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/4 "2017-11-20T15:55:33Z")

</div>

> [@randyzwitch](#):
>
> Do we treat strings as Arrays of Chars elsewhere?

That string isn’t treated as an `Array{Char}` there, but as any iterable:

```julia
append!([1,2,3], ('a' for i in 1:3))

```

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [November 20, 2017, 3:56pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/5 "2017-11-20T15:56:54Z")

</div>

It is a _little_ surprising to me that a `Char` gets automatically converted to an `Int` here. This seems potentially dangerous in this particular case, though I haven’t thought through the broader implications of this.

---

<div class="post-metadata">

### Author: ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)
#### Post date: [November 20, 2017, 3:59pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/6 "2017-11-20T15:59:40Z")

</div>

Sorry for the mistake in nomenclature, but @ExpandingMan is getting at my surprise/question. Have the iterable be decomposed into Chars, and the Chars converted into Int to work with the main array seems odd (rather than having an error).

---

<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: [November 20, 2017, 4:02pm UTC](https://discourse.julialang.org/t/append-to-an-int-array-with-string-bug/7157/7 "2017-11-20T16:02:19Z")

</div>

This is pretty unfortunate but I think it’s a fairly unavoidable consequence of strings being iterable sequences of characters and the fact that `Char` is convertible to `Int`. However, @jeff.bezanson is [working on a change](https://github.com/JuliaLang/julia/pull/23273) for 0.7/1.0 which will allow `Int('x')` to work while `convert(Int, c::Char)` need not be allowed, which would fix this issue.
