# Overload in place =

**URL:** <https://discourse.julialang.org/t/overload-in-place/100331>\
**Category:** General Usage\
**Tags:** question, base\
**Created:** [June 14, 2023, 1:42pm UTC](https://discourse.julialang.org/t/overload-in-place/100331 "2023-06-14T13:42:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veenty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veenty/32/50940_2.png) [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Post date:** [June 14, 2023, 1:42pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/1 "2023-06-14T13:42:27Z")

</div>

I have a custom type that looks like this

```julia
struct foo{T}

A1::Array{T}
A2::Array{T}

end

function Base.:+(x:: foo, y:: foo)

return foo(x.A1 + y.A1, x.A2 + y.A2)

end

function Base.:.=(x:: foo, y:: foo)

x.A1 .= y.A1
x.A2 .= y.A2

end

```

And I would like to do a in place operation like

```julia
foo1 = foo(rand(10), rand(10))
foo2 = foo(rand(10), rand(10))

foo1 .= foo1 + foo2

```

But I’m pretty sure that will create an extra foo struct and I’m not really sure how to implement the equivalent to

```julia
A = rand(10)
B = rand(10)
A .= A .+ B

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [June 14, 2023, 2:03pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/2 "2023-06-14T14:03:21Z")

</div>

If you’re willing to give up the `+` syntax, you could define an in place `add!(x::foo, y::foo)` that modifies `x`.

```julia
function add!(x::foo, y::foo)
    x.A1 .+= y.A1
    x.A2 .+= y.A2
    return x
end

```

---

<div class="post-metadata">

**Author:** ![Veenty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veenty/32/50940_2.png) [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Post date:** [June 14, 2023, 2:15pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/3 "2023-06-14T14:15:45Z")

</div>

I would rather not give up the syntax! I’m implementing my struct so that it behaves like a normal array.  
Maybe making it so that foo \<: AbstractArray will solve it?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 14, 2023, 2:19pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/4 "2023-06-14T14:19:46Z")

</div>

I think you need to overload `broadcast!`

[Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.Broadcast.broadcast)!

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [June 14, 2023, 2:21pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/5 "2023-06-14T14:21:54Z")

</div>

See also [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 14, 2023, 3:50pm UTC](https://discourse.julialang.org/t/overload-in-place/100331/6 "2023-06-14T15:50:08Z")

</div>

The documentation on overloading broadcasting can be found here:

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting](https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting)
