Function that takes vector of parameterically typed values

Let us consider the following types

abstract type A end
struct A1 <: A end
struct A2 <: A end

struct B{T<:A}
    type::T
end

Now what I want to do is write a function as narrowest as possible that accepts both vec1 and vec2 below

b1 = B(A1())
b2 = B(A2())
vec1 = [b1, b1]
vec2 = [b1, b2]

My first attempts was defining function like

sample1(vec::Vector{B}) = ()

but it fails to accepts vec1 because vec1’s type is Vector{B{A1}} and Vector{B{A1}} <: Vector{B} is false. Then, my second attempt was like

sample2(vec::Vector{B{T}}) where T <: A = ()

but this function fails to accept vec2 as Vector{B} <: (Vector{B{T}} where T<:A) is false.

How should I define a function that accepts vec1 and vec2?

Have you tried the following?

sample(vec::Vector{<:B}) = ()

Edit: no, I’m afraid that would not work actually it does work, I don’t know whether this is as narrow as you would like it though.

1 Like