# Difference between vector and vector of vectors

**URL:** <https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152>\
**Category:** New to Julia\
**Created:** [August 27, 2018, 7:21pm UTC](https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152 "2018-08-27T19:21:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)\
**Post date:** [August 27, 2018, 7:21pm UTC](https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152/1 "2018-08-27T19:21:44Z")

</div>

My function `f(x::Vector)` works on a vector of numbers (e.g. `x=[0.1, 0.2]`).

However, I’d like to create another method for _vector of vectors_ (e.g. `x=[[0.1, 0.2], [0.3, 0.4]]`), which uses the vector-of-numbers version.

However, it seems that `x=[0.1, 0.2]` and `x=[[0.1, 0.2], [0.3, 0.4]]` are of the same type: the vector-of-vectors is caught and processed by the first version.

How can I specify vector-of-vectors and vector-of-numbers as types to dispatch on? Thank you!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 27, 2018, 7:33pm UTC](https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152/2 "2018-08-27T19:33:49Z")

</div>

I’d recommend using broadcasting in this case: that’s just `f.([[0.1, 0.2], [0.3, 0.4]])`. Broadcasting works element-wise across the container you pass it, without needing to define an additional method.

* * *

A vector of vectors is indeed just a vector. The difference is in its element type:

```julia
f(x::Vector{<:Number}) = "vector of numbers"
f(x::Vector{<:Vector}) = "vector of vectors"
julia> f([1,2,3])
"vector of numbers"

julia> f([[1,2],[3,4]])
"vector of vectors"

```

But note that this _isn’t_ the same as saying what they contain — it’s just a property of the vector itself. In fact, you can have vectors that contain just numbers and won’t work with the above, because they can contain `Any`thing:

```julia
julia> f(Any[1,2,3])
ERROR: MethodError: no method matching f(::Array{Any,1})
Closest candidates are:
  f(::Array{#s1,1} where #s1<:Real) at REPL[10]:1
  f(::Array{#s1,1} where #s1<:(Array{T,1} where T)) at REPL[10]:2
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)\
**Post date:** [August 27, 2018, 7:57pm UTC](https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152/3 "2018-08-27T19:57:07Z")

</div>

Awesome, thanks for that comprehensive reply!

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [August 28, 2018, 4:17am UTC](https://discourse.julialang.org/t/difference-between-vector-and-vector-of-vectors/14152/4 "2018-08-28T04:17:32Z")

</div>

I would just add a change to the type notation as my preferred one,  
if you wanted to allow a more flexible approach which I don’t particularly recommend,

```julia
f(obj::AbstractVector) = all(elem isa Number for elem ∈ obj) ?
                         f(convert(Vector{reduce(promote_type, typeof(elem) for elem ∈ obj)}, obj)) :
                         throw(ArgumentError("elements should all be numbers"))
f(::AbstractVector{<:Number}) = ...

```

I would just keep the second one.
