# getfield(::Dict,val)

**URL:** https://discourse.julialang.org/t/getfield-dict-val/85150
**Category:** New to Julia
**Tags:** dictionary
**Created:** [August 2, 2022, 8:38am UTC](https://discourse.julialang.org/t/getfield-dict-val/85150 "2022-08-02T08:38:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [August 2, 2022, 8:38am UTC](https://discourse.julialang.org/t/getfield-dict-val/85150/1 "2022-08-02T08:38:56Z")

</div>

The following code (from [here](https://github.com/JuliaLang/julia/issues/25750))

` Base.getproperty(d::Dict, s::Symbol) = s ∈ fieldnames(Dict) ? getfield(d, s) : getindex(d, s)`

makes a dictionary key symbols accessible like a struct fields, i.e.

```julia
julia> D = Dict(:a=>1,:x=>1.1,:arr=>[1,2,3],"str"=>2)
julia> D.x
1.1

```

However within REPL

```julia
julia> getfield(D,:x)
ERROR: type Dict has no field x
...

```

I want to understand why getfield(D,:x) does not work while it is working in the method implementation of Base.getproperty?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 2, 2022, 9:03am UTC](https://discourse.julialang.org/t/getfield-dict-val/85150/2 "2022-08-02T09:03:19Z")

</div>

It doesn’t - the symbol is only forwarded to `getfield` if its indeed a field name. Otherwise the result of `getindex` (i.e. `D[:x]`) is returned.

---

<div class="post-metadata">

### Author: ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)
#### Post date: [August 2, 2022, 9:34am UTC](https://discourse.julialang.org/t/getfield-dict-val/85150/3 "2022-08-02T09:34:57Z")

</div>

Oops … yes I see it now, it is `fieldnames(Dict)` (though even `fieldnames(d)` is also not defined)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [August 2, 2022, 9:41am UTC](https://discourse.julialang.org/t/getfield-dict-val/85150/4 "2022-08-02T09:41:54Z")

</div>

That’s to be expected - `fieldnames` takes in a _type_, not an _instance_.
