# Modify attribute of mutable struct from within

**URL:** <https://discourse.julialang.org/t/modify-attribute-of-mutable-struct-from-within/43977>\
**Category:** New to Julia\
**Created:** [July 30, 2020, 1:40pm UTC](https://discourse.julialang.org/t/modify-attribute-of-mutable-struct-from-within/43977 "2020-07-30T13:40:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johncowk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johncowk/32/8032_2.png) [@Johncowk](https://discourse.julialang.org/u/Johncowk)\
**Post date:** [July 30, 2020, 1:40pm UTC](https://discourse.julialang.org/t/modify-attribute-of-mutable-struct-from-within/43977/1 "2020-07-30T13:40:03Z")

</div>

I would like to have a function inside of a mutable struct that is able to modify the value of the attributes from the struct when called.  
As a test, I tried a simple

```nohighlight
mutable struct Test
    a
    b
    function init_test()
        print(a)
        a = 5
        print(a)
    end
    init_test()
end
t = Test(3,3)

```

However this returns :

```julia
ERROR: LoadError: UndefVarError: a not defined

```

I searched through the doc, github and the forum but didn’t find any way to do this, which is surprising since it seems to be something quite basic. What am I doing wrong ?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 30, 2020, 1:50pm UTC](https://discourse.julialang.org/t/modify-attribute-of-mutable-struct-from-within/43977/2 "2020-07-30T13:50:08Z")

</div>

1. `init_test` runs when the type is **defined** , not when an object of the type is created (instanciated). So your code is running at the wrong time.
2. There’s no way to implicitly refer a field. This isn’t like c++. You always need an object to explicitly refer to the field. You simply need a normal function to set the field if that’s what you want to do.
3. And the function that’ll automatically run when you call the type, well, is type itself. You just need to overload it. And it’s called constructor.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 30, 2020, 1:57pm UTC](https://discourse.julialang.org/t/modify-attribute-of-mutable-struct-from-within/43977/3 "2020-07-30T13:57:38Z")

</div>

maybe you want this behavior?

```julia
julia> mutable struct Test
           a
           b
           function Test(a,b)
               print(a)
               new(5,b)
           end
       end

julia> t = Test(3,3)
3Test(5, 3)

```

[https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods-1](https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods-1)
