# How to overload .=?

**URL:** <https://discourse.julialang.org/t/how-to-overload/55864>\
**Category:** General Usage\
**Created:** [February 23, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-overload/55864 "2021-02-23T17:01:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![verseve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/verseve/32/22231_2.png) [@verseve](https://discourse.julialang.org/u/verseve)\
**Post date:** [February 23, 2021, 5:01pm UTC](https://discourse.julialang.org/t/how-to-overload/55864/1 "2021-02-23T17:01:14Z")

</div>

I would like to overload broadcasted assignment (`.=`). What is the correct way to do this, while still using `.=`? Is this possible?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 23, 2021, 5:09pm UTC](https://discourse.julialang.org/t/how-to-overload/55864/2 "2021-02-23T17:09:23Z")

</div>

> [@verseve](#):
>
> I would like to overload broadcasted assignment

`.=` is syntactic sugar for a call to `broadcast!`, so you could overload `broadcast!`.

Hard to say more without knowing what you are trying to accomplish. e.g. give an example of the code you would _like_ to write using `.=`, and what you would want it to _do_.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 23, 2021, 5:12pm UTC](https://discourse.julialang.org/t/how-to-overload/55864/3 "2021-02-23T17:12:44Z")

</div>

Not quite. `.=` and `broadcast!` lower to the same thing. To overload `.=` you need to hook into the custom broadcasting machinery.

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

---

<div class="post-metadata">

**Author:** ![verseve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/verseve/32/22231_2.png) [@verseve](https://discourse.julialang.org/u/verseve)\
**Post date:** [February 23, 2021, 7:10pm UTC](https://discourse.julialang.org/t/how-to-overload/55864/4 "2021-02-23T19:10:37Z")

</div>

Thanks for the answers, and indeed hooking into the custom broadcasting machinery would solve this (haven’t tried it yet). @stevengj : below a simple example of what we/I are trying to do, basically we want to update interdependent fields of a struct, after initialization.

```julia
@with_kw struct Pars
           a::Vector{Float64}
           b::Vector{Float64} = a .+ 0.5
       end

pars = Pars(a=[0.1,0.5,0.8])

pars.a .= [2.0,3.0,4.0]

```

expected with overload of `.=` : `b: Array{Float64}((3,)) [2.5, 3.5, 4.5] `

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 23, 2021, 8:35pm UTC](https://discourse.julialang.org/t/how-to-overload/55864/5 "2021-02-23T20:35:06Z")

</div>

Ah, that’s not possible yet. It’ll only become possible with [https://github.com/JuliaLang/julia/pull/39473](https://github.com/JuliaLang/julia/pull/39473).

If `pars.b` must _always_ mirror `pars.a`, you could use a “computed” `getproperty` overload instead so it’s only created/computed on demand.
