Why [(a = 1, b = 3), (a = 3, b = 4)] isa Vector{NamedTuple} -> false?

hello! why ?
[(a = 1, b = 3), (a = 3, b = 4)] isa Vector{NamedTuple} # -> false

Because NamedTuple is an abstract type not a concrete type, and your array is an array of concrete named-tuples of the same type. So, it is not an array of that abstract types NamedTuple in general, but an array where the elements are subtypes of NamedTuple. Therefore:

julia> [(a = 1, b = 3), (a = 3, b = 4)] isa Vector{<:NamedTuple}
true

And of course, it is an array of the concrete type specifically:

julia> typeof((a = 1, b = 3))
@NamedTuple{a::Int64, b::Int64}

julia> [(a = 1, b = 3), (a = 3, b = 4)] isa Vector{@NamedTuple{a::Int64, b::Int64}}
true

And, finally, you could create an array supporting general named tuples, by annotating that explicitly:

julia> NamedTuple[(a = 1, b = 3), (a = 3, b = 4)] isa Vector{NamedTuple}
true
6 Likes

Is not NamedTupel is a UnionAll not abstract type?

julia> NamedTuple  |> isabstracttype
false

julia> NamedTuple  isa UnionAll
true
1 Like

Yes, right (although the idea is the same there). I’ll edit the post to remove the reference to abstract types.

1 Like

Also if you wanted to check if a vector has an element type of some kind of NamedTuple then use the following expression.

julia> [(a = 1, b = 3), (a = 3, b = 4)] isa Vector{<: NamedTuple}
true
2 Likes