# Why is type promotion asymmetric in \`append!\`? \`append!(\[1.1\],\[1\])\` vs \`append!(\[1\],\[1.1\])\`

**URL:** https://discourse.julialang.org/t/why-is-type-promotion-asymmetric-in-append-append-1-1-1-vs-append-1-1-1/97531
**Category:** New to Julia
**Created:** [April 16, 2023, 1:16am UTC](https://discourse.julialang.org/t/why-is-type-promotion-asymmetric-in-append-append-1-1-1-vs-append-1-1-1/97531 "2023-04-16T01:16:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [April 16, 2023, 1:16am UTC](https://discourse.julialang.org/t/why-is-type-promotion-asymmetric-in-append-append-1-1-1-vs-append-1-1-1/97531/1 "2023-04-16T01:16:14Z")

</div>

Why is this is fine:

```julia
julia> append!([1.1],[1])
2-element Vector{Float64}:
 1.1
 1.0

```

but this not?

```julia
julia> append!([1],[1.1])
ERROR: InexactError: Int64(1.1)

```

I find this strange when this is fine:

```julia
julia> vcat([1], 1.1)
2-element Vector{Float64}:
 1.0
 1.1

```

And when arrays can have mixed type:

```julia
julia> [1, "a"]
2-element Vector{Any}:
 1
  "a"

```

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [April 16, 2023, 1:48am UTC](https://discourse.julialang.org/t/why-is-type-promotion-asymmetric-in-append-append-1-1-1-vs-append-1-1-1/97531/2 "2023-04-16T01:48:44Z")

</div>

In the first two examples. Append means taking an element and putting it into an existing array. Arrays cannot change type. Therefore Julia tries to convert the element to the type of the array. In the first case (int to float), this is possible:

```julia
julia> append!([1.1],[1])
2-element Vector{Float64}:
 1.1
 1.0

```

But in the other case (float to int) it is not. Note that even in the first case, the type of the array did not change. It is still a `Vector{Float64}` after you added a new element.

```julia
julia> append!([1],[1.1])
ERROR: InexactError: Int64(1.1)

```

The following example uses vcat: “Take these two things and create an array with them.” As you are already creating a new array, it is possible to look for the largest type that can hold all the values and use that one.

```julia
julia> vcat([1], 1.1)
2-element Vector{Float64}:
 1.0
 1.1

```

> And when arrays can have mixed type:

They do, but once they are created, you cannot change the type; that is why your first example fails.

---

<div class="post-metadata">

### Author: ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)
#### Post date: [April 16, 2023, 2:01am UTC](https://discourse.julialang.org/t/why-is-type-promotion-asymmetric-in-append-append-1-1-1-vs-append-1-1-1/97531/3 "2023-04-16T02:01:00Z")

</div>

This makes sense. Your answers helped reinforce a few things. `append!` is not a “symmetric” or “commutative” function. You don’t append one thing _and_ another, you append something _to_ something. So I should be careful about what I am appending and what I am appending to. And `vcat` creates a new array altogether.
