Types of arguments within functions

The Union statement is fine. I would do

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 Strings, 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 Vectors.

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.

1 Like