# \`hasfield\` and \`getkey\` in namedtuple

**URL:** https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616
**Category:** General Usage
**Tags:** question, syntax, base
**Created:** [September 24, 2025, 9:57am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616 "2025-09-24T09:57:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Marco\_Lombardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_lombardi/32/14608_2.png) [@Marco\_Lombardi](https://discourse.julialang.org/u/Marco_Lombardi)
#### Post date: [September 24, 2025, 9:57am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/1 "2025-09-24T09:57:07Z")

</div>

Just a curiosity: is there any particular reason for the slightly odd choice of access functions for named tuples? I would expect to have `hasfield` and `getfield`, or alternatively `haskey` and `getkey`, but I see a mixture of them instead.

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [September 24, 2025, 10:38am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/2 "2025-09-24T10:38:33Z")

</div>

```julia
julia> a, c = 1, 2;

julia> nt = (;a, c)
(a = 1, c = 2)

julia> haskey(nt, :a)
true

julia> haskey(nt, :c)
true

julia> haskey(nt, :b)
false

julia> T = typeof(nt)
@NamedTuple{a::Int64, c::Int64}

julia> hasfield(T, :a)
true

julia> hasfield(T, :c)
true

julia> hasfield(T, :b)
false

```

---

<div class="post-metadata">

### Author: ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)
#### Post date: [September 24, 2025, 10:54am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/3 "2025-09-24T10:54:34Z")

</div>

`NamedTuple`s support retrieving valus via `getproperty`/`getfield` and `getindex`.

See

```julia-auto
nt = (; a=1, b=2)

@assert nt.a == nt[:a]

```

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [September 24, 2025, 11:12am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/4 "2025-09-24T11:12:32Z")

</div>

`getindex`/`setindex` for types that have integer keys (tuples and arrays).

`getproperty`/`setproperty` for types with `Symbol` properties that are known at compile time (structs and named tuples). These can be overloaded for custom types to provide special handling of the dot access to properties.

`getfield`/`setfield` for the same as above, but ~~should~~ cannot be overloaded so you can still access the raw fields.

`getkey`/`setkey` for Dictionary like types whose keys are set dynamically at runtime.

Edit: fix to reflect that get/set field can’t be overloaded. Thanks @sgaure !

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [September 24, 2025, 1:23pm UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/5 "2025-09-24T13:23:08Z")

</div>

> [@mrufsvold](#):
>
> `getfield`/`setfield` for the same as above, but should not be overloaded so you can still access the raw fields.

These are builtins, not generics, and can’t be overloaded.

```julia-auto
julia> Base.getfield(a::NamedTuple, s::Symbol) = 1
ERROR: cannot add methods to builtin function `getfield`

```

---

<div class="post-metadata">

### Author: ![Marco\_Lombardi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_lombardi/32/14608_2.png) [@Marco\_Lombardi](https://discourse.julialang.org/u/Marco_Lombardi)
#### Post date: [September 25, 2025, 8:32am UTC](https://discourse.julialang.org/t/hasfield-and-getkey-in-namedtuple/132616/6 "2025-09-25T08:32:59Z")

</div>

Thank you all @_bernhard, @mrufsvold, and @sgaure. @mrufsvold , your summary recap is really useful.

I know recognize that my confusion was due to erroneously thinking that `hasfield`, similarly to `hasproperty`, could be applied to instances. Instead, it has to be used on types. My bad…
