# Expected Vector{Number}, got a value of type Vector{Int64}

**URL:** <https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439>\
**Category:** General Usage\
**Tags:** error\
**Created:** [May 18, 2024, 6:59pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439 "2024-05-18T18:59:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amnesian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amnesian/32/209210_2.png) [@Amnesian](https://discourse.julialang.org/u/Amnesian)\
**Post date:** [May 18, 2024, 6:59pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/1 "2024-05-18T18:59:03Z")

</div>

So I have this function, but it is giving me a very strange error.

```julia
function runCommands(;programName::String="simulation", dt::Number=0.01, T::Number=2, temps::Vector{Number}, N::Integer, sizeArr::Vector{Integer})
    cmdStrings = map((temp, size) -> `./$programName dt=$(dt)_T=$(T)_temp=$(temp)_N=$(N)_size=$(size)`, temps, sizeArr)
    map(run, cmdStrings)
end
runCommands(temps= [0], N=4, sizeArr=[4])

```

But it gives me this error, and I can’t see why it should be a problem:  
ERROR: TypeError: in keyword argument temps, expected Vector{Number}, got a value of type Vector{Int64}

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 18, 2024, 7:06pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/2 "2024-05-18T19:06:28Z")

</div>

When getting errors, it is best to read carefully and assume the details are correct, even if the reason is still not clear.

In this case, `temps` is indeed a `Vector{Int64}` and a `Vector{Number}` is expected as specified in the definition. As evidence:

```julia
julia> typeof( [0] )
Vector{Int64} (alias for Array{Int64, 1})

```

whereas

```julia
julia> typeof( Number[0] )
Vector{Number} (alias for Array{Number, 1})

```

Perhaps the function definition should go with `temps::Vector{<:Number}` which matches `Vector{Int64}` too.

Better still, the typing of parameters is not mandatory in Julia and it is usually cleaner to just drop them and let the compiler infer and compile the correct version of a function. For example, dropping all parameter types:

```julia
function runCommands(;programName="simulation", dt=0.01, T=2, temps, N, sizeArr)
    cmdStrings = map((temp, size) -> `./$programName dt=$(dt)_T=$(T)_temp=$(temp)_N=$(N)_size=$(size)`, temps, sizeArr)
    map(run, cmdStrings)
end

```

---

<div class="post-metadata">

**Author:** ![Amnesian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amnesian/32/209210_2.png) [@Amnesian](https://discourse.julialang.org/u/Amnesian)\
**Post date:** [May 18, 2024, 7:16pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/3 "2024-05-18T19:16:05Z")

</div>

Thanks, the subtype declaration solved the issue, but why does Julia care if it is Vector{Int64} and not Vector{Number}? Why is there an explicit need for this when x::Number can readily accept any subtypes anyway?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 18, 2024, 7:30pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/4 "2024-05-18T19:30:47Z")

</div>

This is connected to the way the type system in Julia has been defined (since forever) and it is actually quite a good thing. Specifically: `Vector{Int64}` is NOT a subtype of `Vector{Number}`. This property is sometimes called invariant-parametric types. The best thing is to read up on it in the docs:  
[https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types](https://docs.julialang.org/en/v1/manual/types/#man-parametric-composite-types)

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [May 18, 2024, 8:15pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/5 "2024-05-18T20:15:46Z")

</div>

> [@Amnesian](#):
>
> Thanks, the subtype declaration solved the issue, but why does Julia care if it is Vector{Int64} and not Vector{Number}? Why is there an explicit need for this when x::Number can readily accept any subtypes anyway?

In short, `Number` is an abstract type of no definite size. A `Vector{Number}` is therefore stored as a collection of pointers to whatever `Number`s are put in there. On the other hand, an `Int64` is a concrete type of size 8 bytes. A `Vector{Int64}` is therefore a contiguous chunk of memory of 8-byte integers. This enables the compiler to generate much more efficient code.

Specifying a formal argument as `Vector{Number}` requires the actual argument to be just that, a collection of pointers to various types of `Number`s. Specifying it as `Vector{<:Number}` means that the actual argument can be a `Vector` of any number type, in particular a `Vector{Int64}`. The function will be compiled for the actual argument, i.e. there will be a compiled version for `Vector{Int64}` as well as a compiled version for `Vector{Float16}` (if you ever call the function with such thing).

It would be possible to let `Vector{Number}` mean a vector of any number type, i.e. having covariant type parameters, and the parameters work like that for the types `Union` and `Tuple`. There are drawbacks and benefits with everything, but julia has always had invariant type parameters (except for `Union` and `Tuple`).

---

<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:** [May 18, 2024, 8:48pm UTC](https://discourse.julialang.org/t/expected-vector-number-got-a-value-of-type-vector-int64/114439/6 "2024-05-18T20:48:44Z")

</div>

The way I think of it is that a `Vector{Number}` is a `Vector` that can accept and store a number of any type, while `Vector{Int64}` can only store `Int64`. Therefore they cannot be the same. On the other hand `x::Number` can be any type of number.
