Subtypes of `Vector{NamedTuple}`

#1

I’m trying to write a function that handles vectors of NamedTuples differently than vectors of other types, but I can’t get it to dispatch properly.

It seems that there is a problem detecting subtypes of Vector{NamedTuple}. For example, I would expect the following to be true:

julia> typeof([(x=1,)]) <: Vector{NamedTuple}
false

Why doesn’t that work?

Without the Vector, it works fine:

julia> typeof((x=1,)) <: NamedTuple
true
0 Likes

#2

Ah… I found the documentation regarding “invariant” types.

The solution to my problem is to write f(x::Vector{<:NamedTuple}) rather than f(x::Vector{NamedTuple}).

5 Likes