# What's the difference between fields and properties?

**URL:** https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495
**Category:** General Usage
**Tags:** question
**Created:** [July 19, 2018, 2:40pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495 "2018-07-19T14:40:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)
#### Post date: [July 19, 2018, 2:40pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/1 "2018-07-19T14:40:32Z")

</div>

I’m trying to understand this pull request which will be implemented in V0.7/V1.0:

[https://github.com/JuliaLang/julia/pull/25311](https://github.com/JuliaLang/julia/pull/25311)

I read the V0.7 documentation but I coudn’t find any clarification on the specific differences between fields and properties.

In fact the functions getfield and setfield! refer to getproperty and setproperty! but the latter functions are not fully documented. The main difference seems to be that field related functions are in _Core_ and the property related functions are in _Base_. Could someone clarify?

Thanks

---

<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: [July 19, 2018, 4:23pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/2 "2018-07-19T16:23:35Z")

</div>

`fields` are simply the “components” of a struct. The struct

```julia
struct A
   b
   c::Int
end

```

has the fields `b` and `c`. A call `getfield` returns the object that is bound to the field:

```julia
julia> a = A("foo", 3)
A("foo", 3)

julia> getfield(a, :b)
"foo"

```

The dot syntax `a.b` used to “lower” i.e. be the same as writing `getfield(a, :b)`. What has changed now is that `a.b` lowers to `getproperty(a, :b)` with the default fallback

```julia
getproperty(a::Type, v::Symbol) = getfield(a, v)

```

So by default, nothing has changed. However, authors of structs can hook into `getproperty`:

```julia
julia> function Base.getproperty(a::A, v::Symbol)
           if v == :c
               return getfield(a, :c) * 2
           elseif v == :q
               return "q"
           else
               return getfield(a, v)
           end
       end

julia> a.q
"q"

julia> getfield(a, :q)
ERROR: type A has no field q

julia> a.c
6

julia> getfield(a, :c)
3

julia> a.b
"foo"

```

So we can add extra functionality to the dot syntax (dynamically if we want). As a concrete example where this is useful is PyCall where you used to have to write `pyobject[:field]` while it is possible now to implement it such that you can write `pyobject.field`.

---

<div class="post-metadata">

### Author: ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)
#### Post date: [July 20, 2018, 11:45am UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/3 "2018-07-20T11:45:28Z")

</div>

Great answer thanks. So in my updated mental model I think of property related functions as the mid-level abstraction between dot notation and field related functions.

edit: excited to start using this, it will be uber helpful for some things I need. Perfect timing 🕶

---

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [January 15, 2019, 2:13pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/4 "2019-01-15T14:13:15Z")

</div>

would be great if some concise form of @kristoffer.carlsson 's answer could make it into the docstrings.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [May 28, 2019, 8:57am UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/5 "2019-05-28T08:57:54Z")

</div>

So you cannot overload `getfield`?

---

<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: [May 28, 2019, 9:06am UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/6 "2019-05-28T09:06:36Z")

</div>

Indeed not.

```julia
julia> getfield
getfield (built-in function)

```

---

<div class="post-metadata">

### Author: ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)
#### Post date: [March 22, 2020, 5:03pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/7 "2020-03-22T17:03:19Z")

</div>

I think this follow-up question is probably directly related enough to the above that it is useful to have it attached to this thread but feel free (mods) to separate if you think it should be.

@kristoffer.carlsson in your great example you had logical branches which dealt with the cases of the different struct field symbols.

Is it possible to just specify the `getproperty` for just a single symbol without having the branching?

The syntax below, based on your example, hopefully explains what I have in mind (though it doesn’t work as is)  
`Base.getproperty(a::A, :q) = "q"`

---

<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: [March 22, 2020, 8:38pm UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/8 "2020-03-22T20:38:37Z")

</div>

Not really, you would just have to write

```julia
Base.getproperty(a::A, v::Symbol) = v === :q ? "q" : getfield(a, v)

```

---

<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: [March 23, 2020, 7:32am UTC](https://discourse.julialang.org/t/whats-the-difference-between-fields-and-properties/12495/9 "2020-03-23T07:32:56Z")

</div>

> [@moustachio-belvedere](#):
>
> Is it possible to just specify the `getproperty` for just a single symbol without having the branching?

You could consider something like

```julia
Base.getproperty(a::A, key::Symbol) = getproperty(a, Val{key}())
Base.getproperty(a::A, ::Val{K}) where K = getfield(a, K)
Base.getproperty(a::A, ::Val{:q}) = "q"

```

and count on constant folding (I didn’t check).

Also, maintaining a consistent `propertynames` may be tricky.
