# Confused by pass-by-sharing

**URL:** https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233
**Category:** New to Julia
**Created:** [March 1, 2021, 5:52am UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233 "2021-03-01T05:52:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [March 1, 2021, 5:52am UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233/1 "2021-03-01T05:52:43Z")

</div>

In the following simple stylised script,

```julia
mutable struct P <: AbstractP 
    this 
    that
end

p = P()
...
sol1 = optimal(p)
q = p
p = add(p, something) # p changed
sol2 = optimal(p)
p = q
sol3 = optimal(p)

```

would like that sol3 = sol1, but instead got sol3 = sol2. Unfortunatelly, it is still not clear what I should do to get the desired result, that sol3 = sol1? Many thanks!

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [March 1, 2021, 6:00am UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233/2 "2021-03-01T06:00:36Z")

</div>

The following line

```julia
q = p

```

just assigns a different name for `p`, thus if `p` changes later, `q` also changes. I think you need

```julia
q = copy(p)

```

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [March 1, 2021, 12:51pm UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233/3 "2021-03-01T12:51:41Z")

</div>

Thank you for your reply. In my real use case `copy` didn’t work, got `MethodError: no method matching copy(::P)`. But

```julia
q = deepcopy(p)

```

worked.

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [March 1, 2021, 2:03pm UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233/4 "2021-03-01T14:03:03Z")

</div>

If the change in p happens inside a function, like bellow, I also need to do the assign of q to p in global scope (I think) so to have `sol1 == sol3`

```julia
...
sol1 = optimal(p)
function do(p)
    q = deepcopy(p)
    p = add(p, something) # p changed
    sol = optimal(p)
    global p = q # restore p 
    return sol
end
sol2 = do(p)
sol3 = optimal(p)

```

Is there a better way of doing all this? Thanks in advance!

---

<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: [March 1, 2021, 2:16pm UTC](https://discourse.julialang.org/t/confused-by-pass-by-sharing/56233/5 "2021-03-01T14:16:06Z")

</div>

> [@askvorts](#):
>
> In my real use case `copy` didn’t work, got `MethodError: no method matching copy(::P)`

That’s expected, because you need to decide what copying your struct actually means. In your case, that’s probably:

```julia
Base.copy(p::P) = P(copy(p.this), copy(p.that))

```

> [@askvorts](#):
>
> Is there a better way of doing all this?

Sure–at the very least there’s no need to do the `global p = q` thing. Something like this should work without mutating global variables (which is almost always a code smell):

```julia
function foo(p)
  q = copy(p)
  add!(q, something) # I'm assuming that your `add` function actually mutates its argument
  return optimal(q)
end

```

I’m assuming that your `add` function _mutates_ its argument, which is why I’ve renamed it to `add!`. But I’m just guessing here because I can’t actually see your code.

Edit: Fixed some missing `copy()` calls in my `copy` definition
