# Function with Array of Abstract types as arguments

**URL:** https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574
**Category:** General Usage
**Created:** [November 27, 2019, 12:57pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574 "2019-11-27T12:57:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [November 27, 2019, 12:57pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574/1 "2019-11-27T12:57:02Z")

</div>

So, because

```julia
julia> Uniform{Float64} <: Distribution
true

julia> Array{Uniform{Float64},1} <: Array{Distribution}
false

```

I cannot make a function like

```julia
foo(a::Array{Distribution}) = nothing

```

and call it with

```julia
foo([Uniform(0,1)])

```

How do i correctly define foo to make this work ?

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [November 27, 2019, 12:59pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574/2 "2019-11-27T12:59:56Z")

</div>

Oh nevermind, the answer is

```julia
function foo(a::Array{D,1}) where D <: Distribution
nothing
end

```

I was confused because

```julia
function foo(a::Distribution) 
nothing
end

foo(Uniform())

```

works without the need to define as `foo(a <: Distribution)`

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [November 27, 2019, 2:26pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574/3 "2019-11-27T14:26:45Z")

</div>

You can also write `Array{<:Distribution,1}` or `Vector{<:Distribution}`. To be more generic, maybe `AbstractVector{<:Distribution}`.

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [November 27, 2019, 2:33pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574/4 "2019-11-27T14:33:33Z")

</div>

Oh thanks, I guess `where` is useful to make sure multiple arguments receive the same type.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [November 27, 2019, 2:59pm UTC](https://discourse.julialang.org/t/function-with-array-of-abstract-types-as-arguments/31574/5 "2019-11-27T14:59:10Z")

</div>

Right, it is useful when types of arguments should be somehow related to each other: `foo(x::T, v::AbtractArray{T}) where {T<:Real}` or `foo(x::T, y::S) where {T, S<:T}`. Also useful when you want to instantiate an object of a specific argument-dependent type in the function body:

```julia
function foo(v::Vector{K}, w::Vector{V}) where {K,V}
    return Dict{K,V}(v .=> w)
end

```

The difference from just `Dict(v .=> w)` would be that if `v` and `w` are arrays of abstract types but happen to contain elements of a concrete type (say, both are arrays of `Any` but accidentally only contain strings when `foo` is called), `Dict{K,V}` may be what you actually want and a Dict of autodetected key and value types may be not.
