# Passing object by reference and mutating it

**URL:** <https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051>\
**Category:** New to Julia\
**Created:** [July 31, 2019, 7:46pm UTC](https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051 "2019-07-31T19:46:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [July 31, 2019, 7:46pm UTC](https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051/1 "2019-07-31T19:46:39Z")

</div>

A simple MWE:

```julia
mutable struct MyHuman
    age::Int64
    MyHuman(i) = new(i)
end

# create an array
A = [MyHuman(i) for i = 1:100] 

# let's replace the 1st element of A by a new object
change_element(A[1])

function change_element(a::MyHuman)
    # check if the incoming argument refers to the same object
    println("equal before?: $(a === A[1])") ## true
    a = MyHuman()
    a.age = 100000
    println("equal after?: $(a === A[1])") ## false!!!
end

#A[1].age NEVER CHANGES! because I think it creates a new object in memory

```

Basically I have an `Array` of my custom type. I would like to pass in object `A[1]` and essentially replace this object with a new instance of the type.

For more context, consider an agent-based model where agents have an age. After a certain age, the agent dies. To maintain a constant population, I would like to replace the dead agent by a brand new agent in it’s place.

I understand that I don’t have to run the `a = MyHuman()` as this creates a new object in memory and reassigns my variable `a`. I could manually just update the fields of this person, but this will involve repeating code. In reality my `MyHuman()` type has many fields, and the constructor has certain logic that needs to be followed.

Any ideas?

Edit: I guess if I pass in the entire array and reassign `A[1] = MyHuman()` it would work, but I’d like the function to act on a single object rather than the whole array.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 31, 2019, 8:06pm UTC](https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051/2 "2019-07-31T20:06:28Z")

</div>

> [@affans](#):
>
> I understand that I don’t have to run the `a = MyHuman()` as this creates a new object in memory and reassigns my variable `a` .

Yeah, that’s the problem. What you are trying to do is not possible in Julia (or ~~most~~ many other languages, for that matter).

> [@affans](#):
>
> I could manually just update the fields of this person

This seems like exactly the right thing to do. I don’t understand what you mean about this involving repeating code–perhaps if you can describe that in more detail we can help find a solution?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [July 31, 2019, 8:08pm UTC](https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051/3 "2019-07-31T20:08:58Z")

</div>

Firs, your code does not run: `ERROR: MethodError: no method matching MyHuman()`.

Second, it should be

```julia
julia> function change_element(a::MyHuman)
           # check if the incoming argument refers to the same object
           println("equal before?: $(a === A[1])") ## true
           a.age = 100000
           println("equal after?: $(a === A[1])") ## false!!!
       end
julia> change_element(A[1])
equal before?: true
equal after?: true

```

Objects are always passed by value [\*]. The value might be a pointer though; since you declared `MyHuman` as mutable, this is the case. Therefore, `a.age = 1` will mutate the object and do what you want.

[\*] Ok, objects are semantically passed by value; the julia abi sometimes uses pass-by-ref as an optimization, but this is mostly invisible to users.

edit: It is very unhelpful to think about C++ for julia; instead, compare to C. The `mutable` keyword then tells you that you will always deal with pointers to the struct, with the same semantics. Furthermore, assignment to immutable struct fields is not supported in julia, instead you need to write the analogue of `a->age = 100000;`.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 31, 2019, 9:46pm UTC](https://discourse.julialang.org/t/passing-object-by-reference-and-mutating-it/27051/4 "2019-07-31T21:46:43Z")

</div>

I definitely agree with @foobar_lv2 but if you want the behavior you describe you can use a 0-dimensional `view` like this:

```julia
julia> function change_element(a)
           # check if the incoming argument refers to the same object
           println("equal before?: $(a[] === A[1])") ## true
           a[] = MyHuman(0)
           a[].age = 1000000
           println("equal after?: $(a[] === A[1])") ## true
       end
change_element (generic function with 2 methods)

julia> change_element(view(A, 1))
equal before?: true
equal after?: true

```

Note that you have to use `[]` after the `view`’s name to access or change its value (you’re basically just indexing into a 0-dimensional Array).
