# Vcat gives Any

**URL:** <https://discourse.julialang.org/t/vcat-gives-any/102012>\
**Category:** General Usage\
**Created:** [July 24, 2023, 12:18pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012 "2023-07-24T12:18:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 24, 2023, 12:18pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/1 "2023-07-24T12:18:12Z")

</div>

```julia
v = [[1, 2, 3], [4, 5]]

f(v) = vcat(v...)

julia> f(v)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> @code_warntype f(v);
MethodInstance for f(::Vector{Vector{Int64}})
  from f(v) @ Main REPL[87]:1
Arguments
  #self#::Core.Const(f)
  v::Vector{Vector{Int64}}
Body::Union{Vector{Any}, Vector{Int64}}
1 ─ %1 = Core._apply_iterate(Base.iterate, Main.vcat, v)::Union{Vector{Any}, Vector{Int64}}
└── return %1

```

dunno why `vcat()` produces a `Union{Vector{Any}, Vector{Int64}}` rather than just a `Vector{Int64}`? The `Vector{Any}` would generate warntypes in subsequent calls.

Can it be avoided? Thanks

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 24, 2023, 12:34pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/2 "2023-07-24T12:34:41Z")

</div>

I think it’s just the splatting:

```julia
julia> f(v) = reduce(vcat, v)
f (generic function with 1 method)

julia> @code_warntype f(v)
MethodInstance for f(::Vector{Vector{Int64}})
  from f(v) @ Main REPL[13]:1
Arguments
  #self#::Core.Const(f)
  v::Vector{Vector{Int64}}
Body::Vector{Int64}
1 ─ %1 = Main.reduce(Main.vcat, v)::Vector{Int64}
└── return %1

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 24, 2023, 12:49pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/3 "2023-07-24T12:49:31Z")

</div>

Thanks! But _ **why** _ splatting in particular `vcat(v...)` causes the problem? Other use cases (i.e. not using `vcat`) of splatting seem to work fine???

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 24, 2023, 1:08pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/4 "2023-07-24T13:08:08Z")

</div>

It has been said that splatting can be very taxing on the compiler, so maybe there are some heuristics to limit inference in this case to ensure compile times remain reasonable.

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [July 24, 2023, 1:08pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/5 "2023-07-24T13:08:40Z")

</div>

Here, splatting is faster! (julia 1.9.2)

```julia
julia> v=[[1,2,3],[4,5]]
2-element Vector{Vector{Int64}}:
 [1, 2, 3]
 [4, 5]

julia> @btime vcat($v...)
  63.342 ns (1 allocation: 96 bytes)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> @btime reduce(vcat,$v)
  72.224 ns (2 allocations: 176 bytes)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 24, 2023, 1:10pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/6 "2023-07-24T13:10:35Z")

</div>

Yes it may be faster by itself. But the `Any` produced would cause warntypes in subsequent calls.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 24, 2023, 1:12pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/7 "2023-07-24T13:12:51Z")

</div>

hm? In this case the type inference should be straightforward (by a human)…… maybe it should be considered a “bug”?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 24, 2023, 1:32pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/8 "2023-07-24T13:32:06Z")

</div>

I think the `Vector{Any}` comes from the fact that `v` may be empty. Perhaps the handling of the empty case may be improved.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 24, 2023, 1:49pm UTC](https://discourse.julialang.org/t/vcat-gives-any/102012/9 "2023-07-24T13:49:38Z")

</div>

Note that the somewhat hacky way of defining the function `f` doesn’t lead to the inference issue:

```julia
julia> f(v) = vcat(v[1], @view(v[2:end])...)
f (generic function with 1 method)

julia> Test.@inferred f([[1, 2, 3], [4, 5]])
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

This ensures that there is at least one element to `vcat`, which rules out `Vector{Any}`.
