NamedTuples with constant type

You would have to add another type parameter and define your own inner constructor, if you don’t always want to have to specify the third parameter, e.g.:

struct Foo{S,T,N}
    val::NamedTuple{S,NTuple{N,T}}
    function Foo{S,T}(val::NamedTuple{S,NTuple{N,T}}) where {S,T,N}
        return new{S,T,N}(val)
    end
end

It doesn’t make much sense here to restrict S, since that’s already handled by the NamedTuple constructor. You could however always check this in the constructor.

1 Like