Simple type problem

I have an abstract type,

abstract type AT end

and two mutable structs that are subtypes of AT:

mutable struct S1 <: AT
    #something
end

mutable struct S2 <: AT
    #something
end

I would like to define a function that takes as argument vectors that can contain either S1s or S2s or both. I had hoped that the following would work:

function fnc(v::Vector{AT})
    #something
end

However, it turns out that it works only if the vector contains both S1s and S2s. If, for instance, the argument is a Vector{S1} the function throws an error. I’m sure there is a simple solution but I can’t think of it. Help would be much appreciated.

You’ll want to specify a subtype <: relationship for the elements of the vector:

function fnc(v::Vector{<:AT})
2 Likes

Perfect! Thank you very much.