# \`Vector{Float64} \<: Vector{Any}\` = false, why?

**URL:** <https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103>\
**Category:** General Usage\
**Created:** [April 27, 2022, 3:40am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103 "2022-04-27T03:40:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [April 27, 2022, 3:40am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/1 "2022-04-27T03:40:48Z")

</div>

My apologies if this has been covered previously. I just got burned by this, I was assuming I could use `::Vector{Any}` to cover an input of `Vector{Float64}`, `Vector{Int}`, etc. But it turns out that Julia doesn’t believe `Vector{Float64}` is a subtype of `Vector{Any}`, I need to use instead…

```julia
Vector{Float64} <: Any

```

Or it also seems that this works too…

```julia
Vector{Float64} <: Vector

```

What am I missing?

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [April 27, 2022, 3:55am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/2 "2022-04-27T03:55:04Z")

</div>

I don’t know how to answer your main question, but just to add to the discussion:

Running the code below in the REPL gives true.

```julia
julia> Vector{Float64} <: Vector{<:Any}
true

```

If you want to check if a type is subtype of a `Vector` of `Int`s or `Float64`s, you can do something like:

```julia
julia> Vector{Float64} <: Vector{<:Union{Float64, Int}}
true

```

Or better yet:

```julia
julia> Vector{Float64} <: Vector{<:Real}
true

```

I’m not sure how that works though

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [April 27, 2022, 4:07am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/3 "2022-04-27T04:07:14Z")

</div>

This is what was throwing me for a loop…

```julia
function test(x::Vector{Any})
    println(length(x))
end

test(zeros(10))

```

which gives the error:

```julia
ERROR: MethodError: no method matching test(::Vector{Float64})
Closest candidates are:
  test(::Vector{Any})

```

It turns out the notation you used solves the problem…

```julia
function test(x::Vector{<:Any})
    println(length(x))
end

```

Looks like I learned something new, didn’t realize I could use `<:` in the type definition.

Thanks!

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [April 27, 2022, 4:09am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/4 "2022-04-27T04:09:20Z")

</div>

> This last point is _very_ important: even though `Float64 <: Real` we **DO NOT** have `Point{Float64} <: Point{Real}` .  
> In other words, in the parlance of type theory, Julia’s type parameters are _invariant_ , rather than being [covariant (or even contravariant)](https://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29). This is for practical reasons: while any instance of `Point{Float64}` may conceptually be like an instance of `Point{Real}` as well, the two types have different representations in memory:

[https://docs.julialang.org/en/v1/manual/types/#Parametric-Types](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types)

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 27, 2022, 6:02am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/5 "2022-04-27T06:02:12Z")

</div>

This is covered in the [Inheritance section](https://sylvaticus.github.io/SPMLJ/dev/01_-_JULIA1_-_Basic_Julia_programming/0105_-_Custom_types.html#Inheritance) of this (almost ready) course.  
In brief, use templates in these situations.

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [April 27, 2022, 6:20am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/6 "2022-04-27T06:20:57Z")

</div>

This appears to be one of the most frequently asked questions for beginners posted in this forum? I have encountered two similar posts in two days. Do we have a FAQ section or a MUST READ page for beginners in the Julia doc for all these Julia-specific niche issues?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 27, 2022, 6:56am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/7 "2022-04-27T06:56:17Z")

</div>

This might be mainly for practical reasons, but also note that there _is_ actually such a thing as a concrete `Vector{Any}`. Let’s say you have a function that looks like this:

```julia
function foo(x::Vector{Any})
    push!(x, 5)
    push!(x, "Hello")
    return x
end

```

Then

```julia
julia> foo(Any[])
2-element Vector{Any}:
 5
  "Hello"

```

What should happen if you pass in a `Vector{Float64}`?

---

<div class="post-metadata">

**Author:** ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)\
**Post date:** [April 27, 2022, 6:58am UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/8 "2022-04-27T06:58:57Z")

</div>

Another example

```julia
julia> [1, 1.0, :a]
3-element Vector{Any}:
 1
 1.0
  :a

julia> foo(x::Vector{Any}) = length(x)
foo (generic function with 1 method)

julia> foo([1, 1.0, :a])
3

julia> foo([1, 2, 3])
ERROR: MethodError: no method matching foo(::Vector{Int64})
Closest candidates are:
  foo(::Vector{Any}) at REPL[62]:1
Stacktrace:
 [1] top-level scope
   @ REPL[64]:1

```

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [April 27, 2022, 3:28pm UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/9 "2022-04-27T15:28:01Z")

</div>

What helps me understand better is the parametric notation…

```julia
f(x::Vector{T}) where {T === Any} = println("Vector{T} where T is only ::Any i.e. Vector{Any}")
f(x::Vector{T}) where {T <: Any} = println("Vector{T} where T can be anything")
f(x::Vector{T}) where {T === Float64} = println("Vector{T} where T is only ::Float64 i.e. Vector{Float64}")

```

One problem though is `T === Any` doesn’t work. Does anyone know how to properly write this in Julia? I feel like this notation may be a good best practice to avoid confusion.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 27, 2022, 3:47pm UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/10 "2022-04-27T15:47:45Z")

</div>

You can write all of those without where clauses:

```julia
f(x::Vector{Any}) = "Vector{T} where T is only Any"
f(x::Vector{<:Any}) = "Vector{T} where T can be anything"
f(x::Vector{Float64}) = "Vector{T} where T is only Float64"

```

In action:

```julia
julia> f(Any[1])
"Vector{T} where T is only Any"

julia> f(Int[1])
"Vector{T} where T can be anything"

julia> f(Float64[1])
"Vector{T} where T is only Float64"

```

The construct `where {T === X}` isn’t supported because it doesn’t increase expressiveness: you can just eliminate the type parameter `T` entirely by substituting its value everywhere `T` appears. In other words, where clauses are only useful with upper/lower bounds. You can, however, enforce equality by using an upper and lower bound that are the same, so this does what you want using where clauses:

```julia
f(x::Vector{T}) where {Any <: T <: Any} = "Vector{T} where T is only Any"
f(x::Vector{T}) where {T <: Any} = "Vector{T} where T can be anything"
f(x::Vector{T}) where {Float64 <: T <: Float64} = "Vector{T} where T is only Float64"

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 27, 2022, 4:05pm UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/11 "2022-04-27T16:05:37Z")

</div>

My take on that: [Vector{Int} \<: Vector{Real} is false??? · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/typevariance/)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2022, 4:55pm UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/12 "2022-04-27T16:55:52Z")

</div>

> [@liuyxpp](#):
>
> Do we have a FAQ section

See _Why doesn’t it work to declare `foo(bar::Vector{Real}) = 42` and then call `foo([1])`?_ in the [Julia FAQ](https://docs.julialang.org/en/v1/manual/faq/).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 27, 2022, 5:00pm UTC](https://discourse.julialang.org/t/vector-float64-vector-any-false-why/80103/13 "2022-04-27T17:00:58Z")

</div>

Another thread on why parametric types are invariant: [Reason behind designing parametric types as invariant](https://discourse.julialang.org/t/reason-behind-designing-parametric-types-as-invariant/18971). Note that these covariance and contravariace relations hold:

- covariance: `S <: T` ⇒ `P{<:S} <: P{<:T}`
- invariance: `S ≠ T` ⇒ ¬ `P{S} <: P{T}`
- contravariance: `S <: T` ⇒ `P{>:S} >: P{>:T}`
