# Custom updating binary operator

**URL:** <https://discourse.julialang.org/t/custom-updating-binary-operator/29301>\
**Category:** General Usage\
**Created:** [September 29, 2019, 9:32pm UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301 "2019-09-29T21:32:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 29, 2019, 9:32pm UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/1 "2019-09-29T21:32:11Z")

</div>

I have defined `union` for a custom set type.

```julia
Base.union(a::MySet, b::MySet) = ...

```

Then the following work as expected:

```julia
union(a, b)
a ∪ b

```

Is it possible for the updating operator `a ∪= b` to have the expected interpretation of `a = a ∪ b`?

---

<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:** [September 30, 2019, 12:11am UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/2 "2019-09-30T00:11:57Z")

</div>

I don’t think it’s possible. The syntax `x += y` does not call some special `+=` function, but instead it just gets lowered to `x = x + y`, so there is no `+=` function to overload. As far as I know, that lowering rule doesn’t exist for `∪=`, so implementing that behavior isn’t possible.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 30, 2019, 9:10am UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/3 "2019-09-30T09:10:17Z")

</div>

OK thanks, that makes sense.

> [@rdeits](#):
>
> lowering rule doesn’t exist for `∪=`

Is there any reason why lowering rules couldn’t be added for selected binary operators?

---

<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:** [September 30, 2019, 1:08pm UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/4 "2019-09-30T13:08:24Z")

</div>

> [@greg\_plowman](#):
>
> Is there any reason why lowering rules couldn’t be added for selected binary operators?

No technical reason. In fact, adding support for other updating operators would be backwards compatible, so it could even be added in the Julia 1.x timeframe.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 30, 2019, 1:19pm UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/5 "2019-09-30T13:19:35Z")

</div>

I also tried to do exactly this the other day and think this would be nice to have.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 2, 2019, 9:21pm UTC](https://discourse.julialang.org/t/custom-updating-binary-operator/29301/6 "2019-10-02T21:21:00Z")

</div>

There is an existing issue for a feature request to allow more updating operators:

[https://github.com/JuliaLang/julia/issues/15964](https://github.com/JuliaLang/julia/issues/15964)
