NamedTuples with constant type

Dear all,
I’m defining a struct

struct foo{S, T}  
    val::NamedTuple{S}
end

where S is a tuple of Symbols and all the element of the NamedTuple are of type T.
Essentially, if S=(:1, :2), what we are looking at is a

NamedTuple{S, (T, T)}

Since the lenght of S is variable, how can I do something like

struct foo{S, T}  
    val::NamedTuple{S, T^(length(S))}
end

If I could use a mechanism similar to @generate, this would work fine, but it does not seem to work for structs.

Another question is about how can I force the type of S in the parameter, i.e., something like

struct foo{S::Tuple{Symbols}, T}  
    val::NamedTuple{S, T^(length(S))}
end

Thank everybody

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

Thank you! I could adapt my code to work with your solution!