Why is Tuple Covariant / NamedTuple is not?

Just make NT covariant on T:

julia> T = Tuple{Symbol, NTuple{N,Int} where N}
Tuple{Symbol,Tuple{Vararg{Int64,N}} where N}

julia> NT = NamedTuple{(:foo, :bar), <:T}
NamedTuple{(:foo, :bar),var"#s46"} where var"#s46"<:Tuple{Symbol,Tuple{Vararg{Int64,N}} where N}

julia> (foo=:a, bar=(2,3)) isa NT
true

Tuple is the only covariant type in julia, everything else must be made covariant with <: if that’s what you want.

5 Likes