Custom updating binary operator

I have defined union for a custom set type.

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

Then the following work as expected:

union(a, b)
a ∪ b

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

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.

2 Likes

OK thanks, that makes sense.

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.

4 Likes

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

3 Likes

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

https://github.com/JuliaLang/julia/issues/15964

3 Likes