# NamedTuple constructor syntax

**URL:** <https://discourse.julialang.org/t/namedtuple-constructor-syntax/88220>\
**Category:** Internals & Design\
**Tags:** namedtuple, constructors\
**Created:** [October 4, 2022, 9:46am UTC](https://discourse.julialang.org/t/namedtuple-constructor-syntax/88220 "2022-10-04T09:46:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [October 4, 2022, 9:46am UTC](https://discourse.julialang.org/t/namedtuple-constructor-syntax/88220/1 "2022-10-04T09:46:58Z")

</div>

If I define a NamedTuple type like this

```julia
MyTupleType = NamedTuple{(:a, :b), Tuple{Float64, String}}
# or equivalently
MyTupleType = @NamedTuple{a::Float64, b::String}

```

I can use this to construct new named tuples with

```julia
mynamedtuple = MyTupleType((π, "hello"))

```

but not

```julia
mynamedtuple = MyTupleType(π, "hello")

```

Is there a good reason the constructor for the latter does not exist? It was how I assumed it should work and it took me a bit (well, like 20 minutes) to figure out that the former is the correct syntax.

It seems to me simply adding another constructor

```julia
NamedTuple{names,T}(args...) where {names,T<:Tuple} = NamedTuple{names,T}(Tuple(names))

```

should do the job. Are there some gotchas why this is not a good idea to have?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 4, 2022, 10:54am UTC](https://discourse.julialang.org/t/namedtuple-constructor-syntax/88220/2 "2022-10-04T10:54:28Z")

</div>

Yes, there is. A `NamedTuple` is something that provides names to the elements of a tuple, this is the reason that a tuple is expected by the constructor.

In general, Julia does not provide a `varargs` version of constructors that expect a tuple or other collective data structure. This provides encouragement to provide the data structure that `fits` rather than an argument sequence.

You are welcome to define your own shortcut, be careful to check that the number of args matches the number of names. You should not overload `NamedTuple` (avoid type piracy). The functionality you seek is available with the package [NamedTupleTools.jl](https://github.com/JeffreySarnoff/NamedTupleTools.jl) 🙂

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 4, 2022, 4:27pm UTC](https://discourse.julialang.org/t/namedtuple-constructor-syntax/88220/3 "2022-10-04T16:27:58Z")

</div>

There’s an ambiguity with the potential varargs namedtuple constructor: what should NamedTuple{(:a,)}((123,)) return? Is it (a=123,) or (a=(123,),)?
