# Confused about NamedTuple implementation

**URL:** https://discourse.julialang.org/t/confused-about-namedtuple-implementation/83007
**Category:** General Usage
**Created:** [June 19, 2022, 10:12am UTC](https://discourse.julialang.org/t/confused-about-namedtuple-implementation/83007 "2022-06-19T10:12:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)
#### Post date: [June 19, 2022, 10:12am UTC](https://discourse.julialang.org/t/confused-about-namedtuple-implementation/83007/1 "2022-06-19T10:12:23Z")

</div>

Hi,

I wanted to understand how `NamedTuple`s work, so I’ve asked:

```julia
@which NamedTuple((:a=>1,))
# NamedTuple(itr) in Base at namedtuple.jl:122

```

which is apparently this line:

```julia
NamedTuple(itr) = (; itr...)

```

Also, we have that:

```julia
(;)
# NamedTuple()

```

So, NamedTuple is constructing by constructing a named-tuple?

DD

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 19, 2022, 1:14pm UTC](https://discourse.julialang.org/t/confused-about-namedtuple-implementation/83007/2 "2022-06-19T13:14:56Z")

</div>

Convenient syntax is parsed and lowered to sequences of method calls.

```julia
julia> @code_lowered NamedTuple((:a=>1,))
CodeInfo(
1 ─ %1 = Base.NamedTuple()
│ %2 = Base.merge(%1, itr)
└── return %2
)

julia> @which merge(NamedTuple(), (:a=>1,))
merge(a::NamedTuple, itr) in Base at namedtuple.jl:288

```

Using `code_lowered` further doesn’t seem to help because it just says it’s applying `Core.NamedTuple`, so I looked at the source code. It seems to be down to `NamedTuple{(names...,)}((vals...,))` which takes in a `Tuple`, so:

```julia
julia> @which NamedTuple{(:a,)}((1,))
(NamedTuple{names})(args::Tuple) where names in Core at boot.jl:601

```

boot.jl only has a few lines of source code for `NamedTuple`, so you can take a look at that. This has already gotten uncomfortably close to Core and generated functions for me, so I’m not really sure of what exactly happens past this point. I think boot.jl runs before namedtuple.jl does, so I’m _guessing_ the lines in boot.jl is where it calls Core code.

Interestingly, namedtuple.jl has an `@eval`ed method that is very similar to the `eval`ed definition in boot.jl, I think the difference is that it belongs to Base vs Core, though I can’t tell you which happens when.

---

<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: [June 19, 2022, 5:59pm UTC](https://discourse.julialang.org/t/confused-about-namedtuple-implementation/83007/3 "2022-06-19T17:59:14Z")

</div>

This shows us that a NamedTuple is a built-in type that has one or more names  
[hence _Named_Tuple] and becomes constructed when given a tuple of value[s]  
[hence Named_Tuple_] to be assigned to the name[s].

```julia
to_obtain = (three = 3,)

name_to_use = :three
value_to_assign = 3

# we start with the Type, 
# which is specialized by the name[s]
# we finish with the value

NamedTuple_type =
    Core.apply_type(Core.NamedTuple, (name_to_use,))

namedtuple = 
    NamedTuple_type( Core.tuple(value_to_assign) )

namedtuple == (three = 3,)

```
