# Types of arguments within functions

**URL:** <https://discourse.julialang.org/t/types-of-arguments-within-functions/42337>\
**Category:** General Usage\
**Tags:** function\
**Created:** [July 1, 2020, 3:11am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337 "2020-07-01T03:11:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alphasig329](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alphasig329/32/9794_2.png) [@alphasig329](https://discourse.julialang.org/u/alphasig329)\
**Post date:** [July 1, 2020, 3:11am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337/1 "2020-07-01T03:11:59Z")

</div>

I have writing a function where both arguments can either be an array of Bool, Int64, and String values. I would prefer not to specify both arguments (less space) but rather substitute a substitute Type something like this.

```julia
function test(obsv_array::T, pred_array::T) where {T = Union{Array{String,1},Array{Int64,1},Array{Bool,1}}}
     return operation...
end

```

Do you all have any suggestions to do this?

---

<div class="post-metadata">

**Author:** ![alphasig329](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alphasig329/32/9794_2.png) [@alphasig329](https://discourse.julialang.org/u/alphasig329)\
**Post date:** [July 1, 2020, 3:12am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337/2 "2020-07-01T03:12:42Z")

</div>

Also if the Union statement is in poor form please help me a better functional form.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 1, 2020, 3:20am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337/3 "2020-07-01T03:20:18Z")

</div>

The `Union` statement is fine. I would do

```julia
function test(obsv_array::T, pred_array::T) where {T <: Union{<:AbstractVector{<:String},<:AbstractVector{<:Integer}, <:AbstractVector{Bool}}
...
end

```

First, note that I wrote `AbstractString`, and `Integer`. This is so people can pass different types of `String`s, like a `SubString`. Same with `Integer`, which is an abstract type.

Second, note that I wrote `AbstractVector`. This ensures that things like `StaticArrays` can be passed, rather than ordinary `Vector`s.

You can see that this gets complicated quickly, which is why I would encourage you not to restrict your input so much. Julia is flexible, and I encourage you to use that.

For instance, what if someone wants to pass `skipmissing(x)` to your function? This will fail since `skipmissing` is an iterator and not a vector.

---

<div class="post-metadata">

**Author:** ![alphasig329](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alphasig329/32/9794_2.png) [@alphasig329](https://discourse.julialang.org/u/alphasig329)\
**Post date:** [July 1, 2020, 3:30am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337/4 "2020-07-01T03:30:29Z")

</div>

Thank you for the detail explanation. Is there any documentation or videos that explains stuff like this? You have given me a lot of to chew on and this was extremely helpful.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 1, 2020, 4:32am UTC](https://discourse.julialang.org/t/types-of-arguments-within-functions/42337/5 "2020-07-01T04:32:09Z")

</div>

One thing to consider would be whether you want anything more than `function test(obsv_array::AbstractVector{T}, pred_array::AbstractVector{T}) where T`. This is a much looser type check. It only checks that you have 2 vectors with the same eltype. However, unless you have a specific reason to specify types further, this will be just as performant, and is much easier to read.
