# Trouble with multiple dispatch on Arrays

**URL:** <https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884>\
**Category:** General Usage\
**Created:** [January 21, 2019, 4:43pm UTC](https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884 "2019-01-21T16:43:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)\
**Post date:** [January 21, 2019, 4:43pm UTC](https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884/1 "2019-01-21T16:43:33Z")

</div>

Im having some trouble understanding why the following does not work:

```julia
julia> f(x::Array{Integer,1}) = print("test")
f (generic function with 1 method)

julia> f([1,3,3])
ERROR: MethodError: no method matching f(::Array{Int64,1})
Closest candidates are:
  f(::Array{Integer,1}) at REPL[5]:1
Stacktrace:
 [1] top-level scope at none:0

```

As I understand it, `Integer` is a more generic type than `Int64`, so why doesn’t Julia use the method I defined for Integers even if when I enter [1,2,3] into the console it is of type `Array{Int64,1}`

---

<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:** [January 21, 2019, 4:49pm UTC](https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884/2 "2019-01-21T16:49:02Z")

</div>

This is because, to cite the [docs](https://docs.julialang.org/en/v1/manual/types/index.html#Parametric-Composite-Types-1), “in the parlance of type theory, Julia’s type parameters are _invariant_ , rather than being covariant (or even contravariant)”

You probably want:

```julia
julia> f(x::Array{T,1}) where T <: Integer = print("test")
f (generic function with 1 method)

julia> f([1,2,3])
test

```

---

<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:** [January 21, 2019, 5:11pm UTC](https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884/3 "2019-01-21T17:11:46Z")

</div>

You can also write this more compactly as

```julia
f(x::Array{<:Integer,1}) = print("test")

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [January 21, 2019, 9:52pm UTC](https://discourse.julialang.org/t/trouble-with-multiple-dispatch-on-arrays/19884/4 "2019-01-21T21:52:58Z")

</div>

Also,

```julia
julia> f(x::Vector{<:Integer}) = print("test")
f (generic function with 5 methods)

julia> f([1,3,3])
test

```
