# Is the first key of a NamedTuple special?

**URL:** <https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256>\
**Category:** General Usage\
**Created:** [February 27, 2019, 9:56am UTC](https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256 "2019-02-27T09:56:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 27, 2019, 9:56am UTC](https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256/1 "2019-02-27T09:56:26Z")

</div>

I would appreciate it if someone could explain this to me:

```julia
x = (somekey=123, someotherkey="test")
y = (someotherkey="test", somekey=123) # same as x but reversed order

h(x::NamedTuple{K,V}) where {K,V} = :somekey in K ? 3 : nothing

@code_warntype h(x)
println()

@code_warntype h(y)

```

I was expecting all key lookup to happen at compile time since the information is contained in the `NamedTuple` type. Instead I obtain:

```julia
Body::Int64
1 ─ return 3

Body::Union{Nothing, Int64}
1 ── %1 = $(Expr(:static_parameter, 1))::Core.Compiler.Const((:someotherkey, :somekey), false)
│ %2 = (Base.getfield)(%1, 1, false)::Symbol
└─── goto #12 if not true
2 ┄─ %4 = φ (#1 => %2, #11 => %20)::Symbol
│ %5 = φ (#1 => 2, #11 => %21)::Int64
│ %6 = (%4 === :somekey)::Bool
└─── goto #4 if not %6
3 ── goto #13
4 ── %9 = (Base.sle_int)(1, %5)::Bool
└─── goto #6 if not %9
5 ── %11 = (Base.sle_int)(%5, 2)::Bool
└─── goto #7
6 ── nothing
7 ┄─ %14 = φ (#5 => %11, #6 => false)::Bool
└─── goto #9 if not %14
8 ── %16 = (Base.getfield)(%1, %5, false)::Symbol
│ %17 = (Base.add_int)(%5, 1)::Int64
└─── goto #10
9 ── goto #10
10 ┄ %20 = φ (#8 => %16)::Symbol
│ %21 = φ (#8 => %17)::Int64
│ %22 = φ (#8 => false, #9 => true)::Bool
│ %23 = (Base.not_int)(%22)::Bool
└─── goto #12 if not %23
11 ─ goto #2
12 ┄ goto #13
13 ┄ %27 = φ (#3 => true, #12 => false)::Bool
└─── goto #15 if not %27
14 ─ return 3
15 ─ return Main.nothing

```

So it works in the first case, when the key I’m checking for is the first in the given named tuple, but it doesn’t in the second.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 27, 2019, 11:11am UTC](https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256/2 "2019-02-27T11:11:57Z")

</div>

[https://github.com/JuliaLang/julia/issues/28844](https://github.com/JuliaLang/julia/issues/28844)

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 27, 2019, 11:17am UTC](https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256/3 "2019-02-27T11:17:15Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 27, 2019, 1:22pm UTC](https://discourse.julialang.org/t/is-the-first-key-of-a-namedtuple-special/21256/4 "2019-02-27T13:22:51Z")

</div>

Also see [`Base.sym_in`](https://github.com/JuliaLang/julia/blob/2bbb599d2ee45d733826862434751b20cb553209/base/namedtuple.jl#L168) for an implementation that works around this.
