# NamedTuples with constant type

**URL:** <https://discourse.julialang.org/t/namedtuples-with-constant-type/37977>\
**Category:** General Usage\
**Tags:** question, metaprogramming, tuple\
**Created:** [April 21, 2020, 5:48pm UTC](https://discourse.julialang.org/t/namedtuples-with-constant-type/37977 "2020-04-21T17:48:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 21, 2020, 5:48pm UTC](https://discourse.julialang.org/t/namedtuples-with-constant-type/37977/1 "2020-04-21T17:48:40Z")

</div>

Dear all,  
I’m defining a struct

```julia
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

```julia
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

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

```

Thank everybody

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 21, 2020, 6:15pm UTC](https://discourse.julialang.org/t/namedtuples-with-constant-type/37977/2 "2020-04-21T18:15:50Z")

</div>

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.:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Isaia\_Nisoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaia_nisoli/32/12594_2.png) [@Isaia\_Nisoli](https://discourse.julialang.org/u/Isaia_Nisoli)\
**Post date:** [April 21, 2020, 6:43pm UTC](https://discourse.julialang.org/t/namedtuples-with-constant-type/37977/3 "2020-04-21T18:43:28Z")

</div>

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