# Why isn't getproperty defined for Dictionaries?

**URL:** <https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233>\
**Category:** General Usage\
**Tags:** dictionary\
**Created:** [August 3, 2022, 2:12pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233 "2022-08-03T14:12:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 3, 2022, 2:12pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/1 "2022-08-03T14:12:20Z")

</div>

It would be convenient to have the ability to retrieve `Dict` values with dot syntax. Seems like methods could be added to `getproperty` for `Dict{String, <:Any}` and `Dict{Symbol, <:Any}` so that values could be accessed the same way you would a DataFrame column:

```julia
Dict("key" => "value")."key" == "value"
Dict(:key => "value").key == "value"
```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2022, 2:19pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/2 "2022-08-03T14:19:31Z")

</div>

Personally, I think it should be, but there were some objections during the 1.0 release process. Now it might be considered breaking to change the existing meaning of `d.keys` and `d.values`, etc. Of course it can also be argued that fields of types are conventionally private, but that could break quite a lot of code out there.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 3, 2022, 2:20pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/3 "2022-08-03T14:20:23Z")

</div>

Not all keys can be properties. For example `Dict(1=>2)` is fine, but `x.1` is not.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2022, 2:21pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/4 "2022-08-03T14:21:26Z")

</div>

But it could at least work when keys are symbols, so `d.foo` would mean `d[:foo]` which would be extremely useful in many contexts.

---

<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:** [August 3, 2022, 2:34pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/5 "2022-08-03T14:34:54Z")

</div>

And `x.:1` is totally fine: it calls `getproperty(x, 1)`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 3, 2022, 2:38pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/6 "2022-08-03T14:38:02Z")

</div>

Ehh, programming styles that only work for a special cases (or require some “non-standard tricks” like @aplavin showed) are generally something I’d avoid. There’s other issues here too though. `x.foo` has literal information, and so if you know all of the fields already and have that information at compile-time, why not use a NamedTuple? It would be safer, because `x.foo2 = y` would create a new field in the Dictionary while it would error with the NamedTuple, so if you know all of the fields at compile-time well enough to write `x.foo` then you probably want error checking. If you don’t know the field at compile-time, then you’d do `getproperty(x,:foo)`, in which case you might as well do `x[:foo]`.

So I just don’t see where the utility is: either it’s dynamic so use a Dictionary and because it’s dynamic names you need to `x[sym]`, or it’s static so you can `x.foo` in which case you can construct it like `(;foo = y)`.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [August 3, 2022, 4:01pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/7 "2022-08-03T16:01:10Z")

</div>

There are cases where you don’t want to use NamedTuple because you need mutability and also because you have many of these objects and want a unique type to not trigger needlessly many compilation of methods. One example where this dot syntax is convenient if for “configuration”, which can be stored in files for example, which you could load with `YAML` and manipulate with such a configuration dict; in some contexts you want to assume that some keys do exist (and be able to write statically e.g. `config.params`), you might have for example validated the data upstream, and at the same time allow arbitrary keys at whatevel level of the configuration.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 3, 2022, 4:15pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/8 "2022-08-03T16:15:11Z")

</div>

You run into issues shadowing actual properties of `Dicts`, no? `Dict` has properties

```julia
julia> propertynames(Dict())
(:slots, :keys, :vals, :ndel, :count, :age, :idxfloor, :maxprobe)

```

Of course, none of these are part of the public interface so it would _technically_ be possible to bury them under key names (after rewriting all the `Dict` internals to use `getfield` rather than `getproperty`). But having `mydict.count` mean different things depending on whether `:count` is a key seems like a recipe for chaos.

Is `mydict[:count]` really so onerous? `NamedTuple` also supports this syntax, so it’s already a consistent interface for both.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [August 3, 2022, 4:26pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/9 "2022-08-03T16:26:48Z")

</div>

> [@mikmoore](#):
>
> Is `mydict[:count]` really so onerous?

In some cases yes, when you have nested stuff, like `d[:a][:b]` instead of just `d.a.b`, in particular when playing with this at the REPL (well, the REPL-completion is kinda broken for this case, I have a patch for this waiting to be PR’ed…)  
(EDIT: I’m not specifically arguing in favor making `Dict` handle the nice dot syntaxt, just that dot syntax is actually very convenient; I currently just use a custom dict).

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 3, 2022, 4:32pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/10 "2022-08-03T16:32:45Z")

</div>

> [@mikmoore](#):
>
> it’s already a consistent interface for both

Allowing the `.` for `Dict` to work like it does for `NamedTuple` and `DataFrame` would be more consistent …  
The inconsistency while I was coding today is what brought this to my attention.

```julia
# Package Table
df = DataFrame()
df."Quantity" = ["Radial Stress", "Tangential Stress", "Longitudinal Stress", "Equivalent Stress"]
df."Average" = σ_mean
df."Maximum" = σ_max
df."Minimum" = σ_min
df."Max Location" = r[σ_max_index]
df."Min Location" = r[σ_min_index]

# Package Unit Dictionary for Table Columns
df_units = Dict{String, Any}()
df_units["Quantity"] = ""
df_units["Average"] = σ_units
df_units["Maximum"] = σ_units
df_units["Minimum"] = σ_units
df_units["Max Location"] = r_units
df_units["Min Location"] = r_units

```

But I wasn’t aware that Dictionaries had inherent properties, so that is probably a good enough reason not to implement this.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [August 4, 2022, 1:56pm UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/11 "2022-08-04T13:56:00Z")

</div>

[GitHub - JuliaCollections/PropertyDicts.jl](https://github.com/JuliaCollections/PropertyDicts.jl) does this but is out of date. I made a PR a while ago to resolve this but not many people have permissions to repos in JuliaCollections

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [August 24, 2022, 5:35am UTC](https://discourse.julialang.org/t/why-isnt-getproperty-defined-for-dictionaries/85233/12 "2022-08-24T05:35:29Z")

</div>

Frames White (@oxinabox) was kind enough to give me permissions, so PropertyDicts.jl has been updated with a new version.
