# Changing the field value inside the structure

**URL:** <https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 22, 2023, 2:14pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281 "2023-10-22T14:14:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiboko123](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@kiboko123](https://discourse.julialang.org/u/kiboko123)\
**Post date:** [October 22, 2023, 2:14pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/1 "2023-10-22T14:14:32Z")

</div>

Hi everyone,

I have some structure with only one field. Is it possible to declare some function inside this structure that use field variable and change its components.

For example I have following code:

```julia
using Parameters

@with_kw struct MyStruct
    A::Matrix{Int64} = zeros(2, 2)

    function MyStruct( )
        A[1, 1] = 1
    end

end

mystruct = MyStruct()

```

I want to directly use the matrix A defined within the structure and change its values. Can I do something like this : `MyStruct.A[1, 1] = 1` ?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 22, 2023, 2:47pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/2 "2023-10-22T14:47:24Z")

</div>

Do `myStruct.A[1, 1] = 1`. Note that `Mystruct` (capital) is the type.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 22, 2023, 2:50pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/3 "2023-10-22T14:50:19Z")

</div>

I’m a little confused about your question. Let’s start with

```julia
struct MyStruct
  A :: Matrix{Int}
end

```

and the constructor (no need to make that an inner constructor):

```julia
function MyStruct()
      s = MyStruct(zeros(2,2));
      s.A[1,1] = 1;
      return s
end

```

so that

```julia
julia> s1 = MyStruct()
MyStruct([1 0; 0 0])

```

Now you can of course define

```julia
set_a1(s :: MyStruct, x) = s.A[1,1] = x

julia> set_a1(s1, 5)
5

```

Functions other than inner constructors are not defined inside the `struct` (unlike OOP). Not sure if that answers your question, but it may help to clarify what you are looking for.

```julia

```

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [October 22, 2023, 3:04pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/4 "2023-10-22T15:04:42Z")

</div>

> [@kiboko123](#):
>
> I want to directly use the matrix A defined within the structure and change its values. Can I do something like this : `MyStruct.A[1, 1] = 1` ?

As @PetrKryslUCSD said, once you’ve constructed an instance `mystruct` of the Type `MyStruct`, you can manipulate its field `A` using `mystruct.A`.

It won’t bite you here, but worth noting a “gotcha” just in case: generally, if you want to be able to change the values stored in a struct, you want to define it as a `mutable struct`. If field `A` were not a `Matrix`, say an `Int64` instead, then you’d get errors when trying to change its value. In your given case, it still works without the `mutable` keyword only because (under the hood) structs store `Matrix` types using only a `Ref` pointer to the location in memory of their contents; changing the `Matrix` contents doesn’t necessarily change the `Ref` held by the struct.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [October 22, 2023, 8:23pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/5 "2023-10-22T20:23:43Z")

</div>

> [@mike.ingold](#):
>
> It won’t bite you here, but worth noting a “gotcha” just in case

Good point! Put differently, if the field itself is a mutable object, it can still be modified even if it is contained in a normal (immutable) `struct`, but to change the value of an immutable field, a `mutable struct` is necessary.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 22, 2023, 8:38pm UTC](https://discourse.julialang.org/t/changing-the-field-value-inside-the-structure/105281/6 "2023-10-22T20:38:22Z")

</div>

Can’t you just write

```julia
@with_kw struct MyStruct
    A::Matrix{Int64} = [1 0; 0 0]
end

```

?
