# How to define NamedTuple type with variable number of elements?

**URL:** <https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223>\
**Category:** General Usage\
**Created:** [December 28, 2022, 11:52am UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223 "2022-12-28T11:52:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [December 28, 2022, 11:52am UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/1 "2022-12-28T11:52:35Z")

</div>

hello, how to define NamedTuple type with variable number of elements?

```julia
(:a, :b) isa NTuple{N,Symbol} where N # -> true 
(1, 2) isa NTuple{N,Int} where N # -> true

```

why ?

```julia
(a=1,b=2) isa NamedTuple{NTuple{N,Symbol}, NTuple{N,Int}} where N # -> false

```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 28, 2022, 12:08pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/2 "2022-12-28T12:08:03Z")

</div>

A `NTuple` is not the same as a `NamedTuple`.

```julia
julia> (a=1,b=1) |> typeof
NamedTuple{(:a, :b), Tuple{Int64, Int64}}

julia> NamedTuple{(:a,:b)}((1,1))
(a = 1, b = 1)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 28, 2022, 12:31pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/3 "2022-12-28T12:31:29Z")

</div>

To make @fatteneder 's remark perhaps clearer:  
NamedTuple parameters are not types! They are actual tuples.  
So: `NTuple{N,Symbol} where N` is a type, and not the needed parameter for the NamedTuple.

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [December 28, 2022, 12:40pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/4 "2022-12-28T12:40:46Z")

</div>

but

```julia
Tuple{Int, Int} <: NTuple{N,Int} where N # -> true
Tuple{Symbol, Symbol} <: NTuple{N,Symbol} where N # -> true

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 28, 2022, 12:44pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/5 "2022-12-28T12:44:07Z")

</div>

The closest to what you wrote I got is:

```julia
(a=1,b=2) isa NamedTuple{keys, <:Tuple{Vararg{Int,N}}} where {keys, N} # true

```

Note:

```julia
(:a, :b) <: NTuple{N,Symbol} where N # error
(:a, :b) isa NTuple{N,Symbol} where N # true

```

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [December 28, 2022, 12:57pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/6 "2022-12-28T12:57:24Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 28, 2022, 1:00pm UTC](https://discourse.julialang.org/t/how-to-define-namedtuple-type-with-variable-number-of-elements/92223/7 "2022-12-28T13:00:08Z")

</div>

thanks too. I always learn nuances of Julia from thinking about such questions
