# Mutable field in immutable type

**URL:** <https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2017, 12:39pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103 "2017-04-07T12:39:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 7, 2017, 12:39pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/1 "2017-04-07T12:39:05Z")

</div>

I sometimes need to change the value in a single field of an instance of an immutable type. This is especially useful when an instance of this type is spread around different places **and** I need to update one of its values. This way the update occurs everywhere this instance sits, because it’s the same instance.  
The way _I_ accomplish this is by wrapping that specific field in a `Vector`:

```julia
julia> immutable A
           x::Int
           y::Vector{Int} # wrap it
       end
julia> a = A(1,[1])
# a lives in all sorts of places
julia> b = Dict(:b => a)
julia> c = [a, a, b]
julia> println(a, b, c) # the `y`field is equal to 1
A(1,[1])Dict(:b=>A(1,[1]))Any[A(1,[1]),A(1,[1]),Dict(:b=>A(1,[1]))]
julia> a.y[1] = 2
julia> println(a, b, c) # the `y`field is equal to 2, everywhhere!
A(1,[2])Dict(:b=>A(1,[2]))Any[A(1,[2]),A(1,[2]),Dict(:b=>A(1,[2]))]

```

Is there a better way to do this?

---

<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:** [April 7, 2017, 12:57pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/2 "2017-04-07T12:57:37Z")

</div>

Use a mutable type that wraps all the fields you need to mutate. If you only have one, you can abuse `Base.RefValue`.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 7, 2017, 1:04pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/3 "2017-04-07T13:04:09Z")

</div>

Sorry, not sure I understand what you mean:

> Use a mutable type that wraps all the fields you need to mutate.

Wrap them in what, a `Vector`?

> If you only have one, you can abuse Base.RefValue.

Could you please specify how I’d “abuse” `Base.RefValue`?

Thanks!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [April 7, 2017, 1:16pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/4 "2017-04-07T13:16:21Z")

</div>

For the abuse:

```julia
immutable A
x::Int
y::Base.RefValue{Int}
end

```

Why call it abuse? Because there is an ongoing discussion about what Ref/RefValue should be used for…

For the wrapping:

```julia
immutable A
x::Int
mutable1::Int
mutable2::Int
end

```

should become:

```julia
type MyMutables
mutable1::Int
mutable2::Int
end
immutable A
x::Int
mutables::MyMutables
end

```

---

<div class="post-metadata">

**Author:** ![BenjaminBorn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benjaminborn/32/4910_2.png) [@BenjaminBorn](https://discourse.julialang.org/u/BenjaminBorn)\
**Post date:** [November 26, 2018, 1:13pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/5 "2018-11-26T13:13:32Z")

</div>

Sorry for reactivating this old post but Simon’s example doesn’t seem to work in 0.7/1.0. If I do

```julia
struct A
    x::Int
    y::Base.RefValue{Int}
end

a = A(1, 1)

```

I get the message `ERROR: MethodError: Cannot convert an object of type Int64 to an object of type Base.RefValue{Int64}`.

Has the syntax changed or is there a better way now to reset the value of a non-array field in an immutable struct?

---

<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:** [November 26, 2018, 1:16pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/6 "2018-11-26T13:16:32Z")

</div>

```julia
a = A(1, Ref(1))

```

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [December 22, 2019, 8:46am UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/7 "2019-12-22T08:46:53Z")

</div>

Is the use of Base.RefValue still considered an abuse? Or, should I consider it a performant/permanent solution? Thanks!

---

<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:** [December 22, 2019, 2:20pm UTC](https://discourse.julialang.org/t/mutable-field-in-immutable-type/3103/8 "2019-12-22T14:20:43Z")

</div>

I am not quite sure what the abuse is here, except for using an unexported symbol from `Base`. In any case, this is only necessary if one wants to restrict the type, construction can be done with `Ref`, eg for

```julia
struct A{T1,T2}
    x::T1
    y::T2 # a container like a Ref 
end

A(1, Ref(2))

```

If the user does not want to define a type for a mutable wrapper, a 0-dimensional array is also an option:

```julia
struct A{T1,T2}
    x::T1
    y::Array{T2,0}
end

a = A(1, fill(2))
a.y[] = 3

```
