# How to make a function accept children of an abstract type?

**URL:** <https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698>\
**Category:** General Usage\
**Tags:** question, first-steps, function\
**Created:** [January 21, 2021, 1:15am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698 "2021-01-21T01:15:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Visipi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/visipi/32/21206_2.png) [@Visipi](https://discourse.julialang.org/u/Visipi)\
**Post date:** [January 21, 2021, 1:15am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698/1 "2021-01-21T01:15:05Z")

</div>

I have an abstract type “Foo” that has a number of children: “Bar”, “Car”, “Dar”.

I also have a function “f” that takes an array of “Foo” objects as arguments.

```julia
function f(a::Array{Foo, 1})

```

But when I try to run the function with:

```julia
julia> typeof(b)
Bar
julia> f([b])

```

I get an error like:

```julia
ERROR: MethodError: no method matching f(::Array{Bar,1})
Closest candidates are: 
f(::Array{Foo,1})

```

Is there any way to force the function to accept the children of “Foo”? I can’t write methods for every possible array of “Bar” “Dar” and “Car”, so I’m stuck until I can find another solution.

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [January 21, 2021, 1:33am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698/2 "2021-01-21T01:33:27Z")

</div>

Try

```julia
function f(a::Array{<:Foo, 1})

```

or

```julia
function f(a::Array{T, 1}) where T <: Foo

```

instead.

`Array{Foo, 1}` is a specific concrete type where each entry in it can be any subtype of Foo independently from one another. `Array{<:Foo, 1}` on the other hand is the set of all 1D `Array`s which contain elements which are subtypes of Foo.

---

<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:** [January 21, 2021, 1:37am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698/3 "2021-01-21T01:37:33Z")

</div>

> [@WschW](#):
>
> `Array{Foo, 1}` is a specific concrete type where each entry in it can be any subtype of Foo independently from one another.

Can you provide an example of that? I am not sure that I understand the difference.

---

<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:** [January 21, 2021, 1:49am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698/4 "2021-01-21T01:49:22Z")

</div>

> [@lmiq](#):
>
> Can you provide an example of that? I am not sure that I understand the difference

```julia
julia> Number[5; 1.2; π]
3-element Array{Number,1}:
 5
 1.2
 π = 3.1415926535897...

julia> Number[5; 1.2; π] isa Vector{Number}
true

julia> [5; 1.2; π]
3-element Array{Float64,1}:
 5.0
 1.2
 3.141592653589793

julia> [5; 1.2; π] isa Vector{Number}
false

julia> [5; 1.2; π] isa Vector{<:Number}
true

```

---

<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:** [January 21, 2021, 1:53am UTC](https://discourse.julialang.org/t/how-to-make-a-function-accept-children-of-an-abstract-type/53698/5 "2021-01-21T01:53:35Z")

</div>

```julia
julia> A = Number[5; 1.2; π]
3-element Array{Number,1}:
 5
 1.2
 π = 3.1415926535897...

julia> typeof.(A)
3-element Array{DataType,1}:
 Int64
 Float64
 Irrational{:π}

julia> typeof(A)
Array{Number,1}

julia> B = [5; 1.2; π]
3-element Array{Float64,1}:
 5.0
 1.2
 3.141592653589793

julia> typeof.(B)
3-element Array{DataType,1}:
 Float64
 Float64
 Float64

julia> typeof(B)
Array{Float64,1}

```
