# Proper Way to overload operators

**URL:** <https://discourse.julialang.org/t/proper-way-to-overload-operators/19872>\
**Category:** General Usage\
**Created:** [January 21, 2019, 2:20pm UTC](https://discourse.julialang.org/t/proper-way-to-overload-operators/19872 "2019-01-21T14:20:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)\
**Post date:** [January 21, 2019, 2:20pm UTC](https://discourse.julialang.org/t/proper-way-to-overload-operators/19872/1 "2019-01-21T14:20:01Z")

</div>

Hello,

I have been recently using julia and I want to know what the “proper” way of overloading operators is.

Right now whwat I’m doing is defining a module and, inside that module i have basically the following:

```julia
module mymodule
import Base : +,-,*,/ .....
.....
export +,-,*,/

struct MyStruct
end

function +(x::MyStruct, y::MyStruct)
...
end

.....
end

```

And then for each operator I want to define in addition to implementing the method I have to remember to modify what I import and export from my module.

Is there a cleaner way to do this?

---

<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:** [January 21, 2019, 2:25pm UTC](https://discourse.julialang.org/t/proper-way-to-overload-operators/19872/2 "2019-01-21T14:25:23Z")

</div>

Just do

```julia
function Base.:+(x::MyStruct, y::MyStruct)
    ...
end

```

No `import` or `export` required. (`export` is not required in any case, because `Base` exports are already visible to any caller.) This idiom has the added benefit that it is clear at the function-definition site that you are modifying a `Base` function.

Note that it is `Base.:+`, note `Base.+`, to distinguish it from a call `Base .+ (...)` to `.+`.

---

<div class="post-metadata">

**Author:** ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)\
**Post date:** [January 21, 2019, 3:15pm UTC](https://discourse.julialang.org/t/proper-way-to-overload-operators/19872/3 "2019-01-21T15:15:39Z")

</div>

Hi,

This seems much more sensible, thanks!

Quick question though, what does the `:` do at `Base.:+` exactly? I understand that it is to disambiguate from a call to the broadcasted `+` but how does it prevent it? Is the `+` function actually defined in Base as `:+`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2019, 3:27pm UTC](https://discourse.julialang.org/t/proper-way-to-overload-operators/19872/4 "2019-01-21T15:27:15Z")

</div>

`:` is used to “quote” symbols after `.`, it is part of the syntax (cf quoting for symbols, eg `:a`, `:b`). It is not necessary, but you could quote eg

```julia
julia> Base.:sum
sum (generic function with 13 methods)

```
