# Is it possble for Julia to overwrite how to get field

**URL:** <https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507>\
**Category:** General Usage\
**Created:** [April 17, 2021, 11:40pm UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507 "2021-04-17T23:40:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oyczyco13](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@oyczyco13](https://discourse.julialang.org/u/oyczyco13)\
**Post date:** [April 17, 2021, 11:40pm UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/1 "2021-04-17T23:40:19Z")

</div>

```julia
struct A
    obj::B
    field1
    field2
    field3
end

struct B
    field4
    field5
end

b = B(4, 5)
a = A(b, 1, 2 ,3)

a.field4

```

I expect `a` is able to get field4 from a.b (if there is one) by calling `a.field4`, just like I can do it through rewrite ` __getattr__ ` when using python. Is it possible in Julia?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 17, 2021, 11:47pm UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/2 "2021-04-17T23:47:52Z")

</div>

Yeah. You just need to write a new method for `getproperty(x::A, y::symbol)`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 17, 2021, 11:55pm UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/3 "2021-04-17T23:55:06Z")

</div>

See [`Base.getproperty`](https://docs.julialang.org/en/v1/base/base/#Base.getproperty), [`Base.setproperty!`](https://docs.julialang.org/en/v1/base/base/#Base.setproperty!), and [`Base.propertynames`](https://docs.julialang.org/en/v1/base/base/#Base.propertynames). Also note that if you encounter stack overflow problems is probably because you are accessing the own object with the `obj.field` notation inside the function (so you end in an endless recursive call), you should try using [`Base.getfield`](https://docs.julialang.org/en/v1/base/base/#Core.getfield) and [`Base.setfield!`](https://docs.julialang.org/en/v1/base/base/#Core.setfield!) inside your rewritten function (these two cannot be redefined and are what the `Base.getproperty` and `Base.setproperty!` call under the hood while they are not redefined).

---

<div class="post-metadata">

**Author:** ![oyczyco13](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@oyczyco13](https://discourse.julialang.org/u/oyczyco13)\
**Post date:** [April 18, 2021, 12:16am UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/4 "2021-04-18T00:16:35Z")

</div>

@Oscar_Smith @Henrique_Becker Thanks for reply

---

<div class="post-metadata">

**Author:** ![oyczyco13](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@oyczyco13](https://discourse.julialang.org/u/oyczyco13)\
**Post date:** [April 18, 2021, 2:32am UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/5 "2021-04-18T02:32:50Z")

</div>

Hi I wrote the function using `hasfield` and `getfield` like

```julia
function Base.getproperty(a::A, v::Symbol)
    if hasfield(typeof(a.b), v) # the line where sof happens
        getfield(a.b, v)
    else
        getfield(a, v)
    end
end

```

But I still got stackoverflow… It seems like because I’m using `typeof`. Why does that happen and how to solve?  
I must use `typeof` because `B` is an abstract type that may have many implementations with different fields.

---

<div class="post-metadata">

**Author:** ![tchr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tchr/32/15138_2.png) [@tchr](https://discourse.julialang.org/u/tchr)\
**Post date:** [April 18, 2021, 2:34am UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/6 "2021-04-18T02:34:47Z")

</div>

You are using `a.b`: should probably be `getfield(a, :b)`

---

<div class="post-metadata">

**Author:** ![oyczyco13](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@oyczyco13](https://discourse.julialang.org/u/oyczyco13)\
**Post date:** [April 18, 2021, 2:40am UTC](https://discourse.julialang.org/t/is-it-possble-for-julia-to-overwrite-how-to-get-field/59507/8 "2021-04-18T02:40:57Z")

</div>

thank you!
