# How can I "flag" that I mutated a field in a struct such as a vector

**URL:** https://discourse.julialang.org/t/how-can-i-flag-that-i-mutated-a-field-in-a-struct-such-as-a-vector/96099
**Category:** General Usage
**Created:** [March 15, 2023, 1:24am UTC](https://discourse.julialang.org/t/how-can-i-flag-that-i-mutated-a-field-in-a-struct-such-as-a-vector/96099 "2023-03-15T01:24:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kylebeggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylebeggs/32/43348_2.png) [@kylebeggs](https://discourse.julialang.org/u/kylebeggs)
#### Post date: [March 15, 2023, 1:24am UTC](https://discourse.julialang.org/t/how-can-i-flag-that-i-mutated-a-field-in-a-struct-such-as-a-vector/96099/1 "2023-03-15T01:24:42Z")

</div>

Suppose I have a struct where I am mutating one of the fields (say it is a `Vector`) like

```julia
struct A
    x::Vector
end

a = A([1, 2])
a.x[1] = 0

```

and I want to have some sort of flag to indicate whether I have mutated `a.x`. How can I do this? Currently, I am doing

```julia
struct A
    x::Vector
    flag::Vector{Bool}
end

```

where `A.flag` is just a 1-element vector so that it is mutable and I can update it. I’m sure this is not the best way to do this.

I am aware of Accessors.jl, but I don’t want to copy/construct a new object when I update. I also looked at Observables.jl, but the docs are sparse and it is not obvious to me how I would implement this. Any ideas and help is appreciated!

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [March 15, 2023, 2:06am UTC](https://discourse.julialang.org/t/how-can-i-flag-that-i-mutated-a-field-in-a-struct-such-as-a-vector/96099/2 "2023-03-15T02:06:40Z")

</div>

Here is one approach.

```julia
julia> struct A{T}
           x::Vector{T}
           flag::Base.RefValue{Bool}
           A(v) = new{eltype(v)}(v, Ref(false))
       end

julia> a = A([1,2])
A{Int64}([1, 2], Base.RefValue{Bool}(false))

julia> a.x[1] = 2
2

julia> a.flag[] = true
true

julia> a
A{Int64}([2, 2], Base.RefValue{Bool}(true))

```

I parameterized the `struct A` with `T` so that `x` will have a concrete type `Vector{T}`. `x::Vector` by itself is an abstract type and is not very compiler friendly.

`Ref` is basically a 0-dimensional array with one element. It is slightly more space efficient than your version. Here I used `Base.RefValue` since it is a concrete type while `Ref` is an abstract type.

I might go further and implement `Base.setindex!` for your new type. Here you can do an indexed assignment on `a` directly, and this will be forwarded to modify the field `x`. While doing so, it will set the `flag`.

```julia
julia> function Base.setindex!(A, X, inds...)
           setindex!(A.x, X, inds...)
           A.flag[] = true
       end

julia> a = A([1,2])
A{Int64}([1, 2], Base.RefValue{Bool}(false))

julia> a[1] = 3
3

julia> a
A{Int64}([3, 2], Base.RefValue{Bool}(true))

```

---

<div class="post-metadata">

### Author: ![kylebeggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylebeggs/32/43348_2.png) [@kylebeggs](https://discourse.julialang.org/u/kylebeggs)
#### Post date: [March 15, 2023, 1:05pm UTC](https://discourse.julialang.org/t/how-can-i-flag-that-i-mutated-a-field-in-a-struct-such-as-a-vector/96099/3 "2023-03-15T13:05:23Z")

</div>

Exactly what I was looking for, thank you!
