Parametric type with a constrained NamedTuple

I would like to define a parametric type that constrains a NamedTuple to have values of a certain type, but I am running into a problem. I am not sure if it is a bug, and if there is a workaround (other than specifying the length as a type parameter).

MWE:

julia> struct ExampleType{N, T, S <: NamedTuple{N,Tuple{Vararg{T}}}}
       x::T
       y::S
       end

julia> ExampleType{(:a,:b),Float64}
ERROR: NamedTuple names and field types must have matching lengths
Stacktrace:
 [1] top-level scope at REPL[69]:1

julia> VERSION
v"1.4.0-rc1.0"

Does struct ExampleType{N, T, S <: NamedTuple{N,<:Tuple{Vararg{T}}}} work?

3 Likes

Yes, it does, thanks!

I overlooked the fact that NamedTuple isn’t a builtin, but a parametric type like any other (in a sense).

1 Like