# Type inference for vcat

**URL:** https://discourse.julialang.org/t/type-inference-for-vcat/10169
**Category:** General Usage
**Created:** [April 5, 2018, 1:05am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169 "2018-04-05T01:05:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [April 5, 2018, 1:05am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/1 "2018-04-05T01:05:39Z")

</div>

Hi all,

I’m just trying to understand the rules for type inference when using `vcat`. Consider the following operations on v0.7:

```julia
vcat(Int[], 1)
vcat(String[], "a")
vcat(UnitRange{Int}[], 1:1)
vcat(Vector{Int}[], [1])

```

return respectively a `Vector{Int}`, `Vector{String}`, `Vector{Any}`, and `Vector{Any}`. Observing the first two operations, I would have thought the rule was for any type `T`, inputting values with types `(Vector{T}, T)` to `vcat` would return `Vector{T}`, but type inference seems to break down for the last two operations. Is this a bug, or indicative of some subtler behaviour that I’m not seeing?

EDIT: even weirder, note that:

```julia
julia> vcat([1:1], 1:1)
2-element Array{Any,1}:
  1:1
 1   

```

How did I get an `Int` as the second element of the output!?! That _definitely_ feels like a bug.

Cheers,

Colin

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 5, 2018, 4:47am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/2 "2018-04-05T04:47:59Z")

</div>

> [@colintbowers](#):
>
> `vcat(Vector{Int}[], [1])`

I think you meant to write:

```julia

julia> vcat(Int[], [1])
1-element Array{Int64,1}:
1

```

or

```julia
julia> vcat(Vector{Int}[]..., [1]) 
1-element Array{Int64,1}:  
1

```

> [@colintbowers](#):
>
> How did I get an Int as the second element of the output!?!

Hm, what would you have expected? `vcat` makes a vector consisting of all the elements of the first input and then all the elements of the second input. All elements of `1:1` are just `1`.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [April 5, 2018, 5:22am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/3 "2018-04-05T05:22:10Z")

</div>

> [@mauro3](#):
>
> I think you meant to write:
> 
> julia\> vcat(Int, [1])  
> 1-element Array{Int64,1}:  
> 1

No, I definitely meant `vcat(Vector{Int}[], [1])`, since it was the input type pattern `(Vector{T}, T)` I was targeting in my test cases (in this case, `T` is `Vector{Int}`).

The behaviour I was expecting was for a call to `vcat` with input types `(Vector{T}, T)` to always result in `Vector{T}` output, eg I was expecting `vcat([1:1], 1:1)` to return `[1:1, 1:1]`. But what you said here

> [@mauro3](#):
>
> vcat makes a vector consisting of all the elements of the first input and then all the elements of the second input. All elements of 1:1 are just 1.

explains the source of my misunderstanding. My heuristic output type rule derived from the first two test-cases breaks down when `T` is a subtype of `AbstractArray`. And I can see the logic now for doing it the way it is done. It allows us to do really nice things like `vcat([1,2,3], 4:6)`. In my head, I was imagining `vcat([1,2,3], 4:6)` would need to be written `vcat([1,2,3], collect(4:6))`.

Thanks for clearing up my misunderstanding. The only thing I’m still a bit hesitant about is why `vcat(Int[], 1)` is allowed at all? A scalar doesn’t really have any elements, so shouldn’t this op be an error? Or am I asking something that has been hashed out in a github issue long ago and doesn’t need to be revived? 🙂

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 5, 2018, 6:33am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/4 "2018-04-05T06:33:34Z")

</div>

> [@colintbowers](#):
>
> A scalar doesn’t really have any elements, so shouldn’t this op be an error?

That was indeed discussed at length on github, but the conclusion was that numbers are iterable:

```julia
julia> for i in 1
       @show i
       end
i = 1  

```

Thus they should be `vcat`able.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [April 5, 2018, 6:51am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/5 "2018-04-05T06:51:01Z")

</div>

Understood. Thanks again for taking the time to explain.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 5, 2018, 6:54am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/6 "2018-04-05T06:54:06Z")

</div>

You’re welcome. I think there is a “solved”-thingy you can click.

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [April 5, 2018, 5:44pm UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/7 "2018-04-05T17:44:05Z")

</div>

Note that inference isn’t involved here, these are actually promotion rules.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [April 6, 2018, 1:48am UTC](https://discourse.julialang.org/t/type-inference-for-vcat/10169/8 "2018-04-06T01:48:04Z")

</div>

Yes, this whole thread really boils down to me thinking it was an inference issue and not a promotion issue 🙂
