# Why \[1, 2, 3\] is not a Vector{Number}?

**URL:** <https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645>\
**Category:** New to Julia\
**Tags:** question, parametric-types\
**Created:** [December 31, 2020, 8:26am UTC](https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645 "2020-12-31T08:26:16Z")\
**Posts on this page:** 2\
**Page:** 3

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [December 9, 2022, 3:20pm UTC](https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645/41 "2022-12-09T15:20:25Z")

</div>

It is all about whether the container (or container of containers) holds abstract types (versatile but slow) or concrete types (restrictive but fast).

```julia
v1 = [1, 2] # 2-element Vector{Int64}
v2 = [1.1, 2.2] # 2-element Vector{Float64}
vv1 = Vector{<:Number}[] # 0-element Vector{Vector{<:Number}}
push!(vv1, v1) # 1-element Vector{Vector{<:Number}}
push!(vv1, v2) # 2-element Vector{Vector{<:Number}}
vv2 = Vector{Int64}[] # 0-element Vector{Vector{Int64}}
push!(vv2, v1) # 1-element Vector{Vector{Int64}}
push!(vv2, v2) # Error: vv2 container can only hold Vector{Int64} elements
vv1 isa Vector{Vector{<:Number}} # true
vv2 isa Vector{Vector{<:Number}} # false
vv1 isa Vector{<:Vector{<:Number}} # true
vv2 isa Vector{<:Vector{<:Number}} # true

```

I asked a related question [here](https://discourse.julialang.org/t/vector-of-vectors-type-issue-and-tuple-question/46640) which may also be useful to you.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 9, 2022, 3:36pm UTC](https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645/42 "2022-12-09T15:36:37Z")

</div>

First, let’s construct a simple `Vector{Vector{Float64}}`

```julia
# Simple Vector{Vector{Float64}}
julia> a = [[3.0]]
1-element Vector{Vector{Float64}}:
 [3.0]

julia> typeof(a)
Vector{Vector{Float64}} (alias for Array{Array{Float64, 1}, 1})

```

Is `a` a subtype of Vector?

```julia
julia> typeof(a) <: Vector
true

```

Is `a` a subtype of Vector of Vectors with unspecified element type?

```julia
julia> typeof(a) <: Vector{Vector}
false

```

Is `a` a subtype of Vector with element types that are a subtype of Vector?

```julia
julia> typeof(a) <: Vector{<: Vector}
true

```

Is `a` a subtype of Vector with element types that are a subtype of Vector with element type Number?

```julia
julia> typeof(a) <: Vector{<: Vector{Number}}
false

```

Is `a` a subtype of Vector with element types that are a subtype of Vector with element type Float64?

```julia
julia> typeof(a) <: Vector{<: Vector{Float64}}
true

```

Is `a` a subtype of Vector with element types that are a subtype of Vector with an element type that is a  
subtype of Number?

```julia
julia> typeof(a) <: Vector{<: Vector{<: Number}}
true

```

Let’s go through the false answers and create the appropriate types for them to be true.

```julia
julia> vv = Vector[[3.0] ]
1-element Vector{Vector}:
 [3.0]

julia> typeof(vv) <: Vector{Vector}
true

julia> vvn = Vector{Number}[Number[3.0] ]
1-element Vector{Vector{Number}}:
 [3.0]

julia> typeof(vvn) <: Vector{<: Vector{Number}}
true

```

Why does this matter?

In the first case with `a :: Vector{Vector{Float64}} ` we can figure out that the first element of the first vector will be of type `Float64`.

![image](https://global.discourse-cdn.com/julialang/original/3X/9/7/978a4a7c17c104cfb63cc406ffb4a72a30756546.png)

In the case case with `vv :: Vector{Vector}` we do not know the element type at all. It could be anything. We’re as clueless as Python in this case. `@code_warntype` will warn us about this situation by highlighting the uncertainty in type information in red.

![image](https://global.discourse-cdn.com/julialang/original/3X/d/c/dc90e12b0cad7f065e4d013ea8c1a4932cd0dae1.png)

If we specify `Number` as the element type we know a little information, but not specifically that the element type is `Float64`:

![image](https://global.discourse-cdn.com/julialang/original/3X/3/e/3e2d5d7556132e668ec1ce1e2d71cffa5ba80e35.png)

Why does it matter if we know the element type? In subsequent code, it would be best if we know the element type so that we can compile the actual machine instructions to use as well as some hints about how the numbers are arranged in memory. If we do not know the type, we cannot make performance enhancing instructions about memory and we cannot figure what exact machine instructions to use.

I will leave it to Professor Steven G. Johnson to explain further.

> **[Lecture 23: High Performance in Dynamic Languages | Performance Engineering...](https://ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/resources/lecture-23-high-performance-in-dynamic-languages/)**
>
> MIT OpenCourseWare is a web based publication of virtually all MIT course content. OCW is open and available to the world and is a permanent MIT activity

[Previous page](https://discourse.julialang.org/t/why-1-2-3-is-not-a-vector-number/52645.md?page=2)
