# Overloading operators

**URL:** <https://discourse.julialang.org/t/overloading-operators/6773>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 30, 2017, 1:51pm UTC](https://discourse.julialang.org/t/overloading-operators/6773 "2017-10-30T13:51:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![UserMorocco](https://avatars.discourse-cdn.com/v4/letter/u/ee59a6/32.png) [@UserMorocco](https://discourse.julialang.org/u/UserMorocco)\
**Post date:** [October 30, 2017, 1:51pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/1 "2017-10-30T13:51:11Z")

</div>

Hello,

How I can oveload base operators in Julia ?

Thanks

---

<div class="post-metadata">

**Author:** ![ksmcreynolds](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksmcreynolds/32/860_2.png) [@ksmcreynolds](https://discourse.julialang.org/u/ksmcreynolds)\
**Post date:** [October 30, 2017, 2:24pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/2 "2017-10-30T14:24:42Z")

</div>

To extend a method from `Base` you need to import it first. To import the method `fun` you can use either `import Base.fun` or `import Base: fun`. The second is a bit easier if you want to import multiple methods. Then you can define a new method for your type with the normal syntax `fun(a::YourType) = somethingnew`.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 30, 2017, 2:33pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/3 "2017-10-30T14:33:17Z")

</div>

Operators can be defined just like normal functions. For example

```julia
import Base.+

struct TestType end

(+)(::TestType, ::Void) = nothing

```

---

<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:** [October 30, 2017, 2:34pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/4 "2017-10-30T14:34:06Z")

</div>

> [@ksmcreynolds](#):
>
> To extend a method from Base you need to import it first.

Not necessarily: qualifying with package names also works. Eg

```julia
Base.:+(x::SomeType, y::SomeType) = ...

```

Note the quote (`:`) before the `+`.

---

<div class="post-metadata">

**Author:** ![UserMorocco](https://avatars.discourse-cdn.com/v4/letter/u/ee59a6/32.png) [@UserMorocco](https://discourse.julialang.org/u/UserMorocco)\
**Post date:** [October 30, 2017, 3:01pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/5 "2017-10-30T15:01:42Z")

</div>

I write this code to oveload this operator \>= .

```julia
julia>import Base;
julia> Base.:>=(a::JuMP.Variable,b::Float64) = (a >= b)

julia> temp_in >=9.00

```

But I still have this error 😓

```julia
ERROR: MethodError: no method matching isless(::Float64, ::JuMP.Variable)
Closest candidates are:
  isless(::Float64, ::Float64) at float.jl:439
  isless(::PyCall.PyObject, ::Any) at /home/monpc/.julia/v0.6/PyCall/src/pyoperators.jl:71
  isless(::AbstractFloat, ::AbstractFloat) at operators.jl:96
  ...
Stacktrace:
 [1] >=(::JuMP.Variable, ::Float64) at ./REPL[138]:1

```

How I can resolve it ?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 30, 2017, 3:17pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/6 "2017-10-30T15:17:06Z")

</div>

Look at the signature in the error: you did the wrong one. Flip the order.

---

<div class="post-metadata">

**Author:** ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)\
**Post date:** [November 12, 2021, 10:30pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/7 "2021-11-12T22:30:27Z")

</div>

In case anyone is looking to overload `==` and finds this post, you can do so with

```julia
Base.:(==)(a::MyType, b::MyType) = ...

```

When trying to do `Base.:==` from the REPL, it doesn’t evaluate (expecting more input), and from a script, I get the confusing message

> LoadError: syntax: type declarations on global variables are not yet supported

Note this is for Julia 1.6.3, I don’t know if this has changed in recent history.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 12, 2021, 10:45pm UTC](https://discourse.julialang.org/t/overloading-operators/6773/8 "2021-11-12T22:45:25Z")

</div>

```julia
import Base: ==, +, -, *, /, \, inv, ^, <, <=, <<, >>, %, ÷

struct DFP{N} <: Real
    s::Int8
    expo::BigInt
    m::Array{Int8,1}
end

+(x::DFP{N}, y::DFP{N}) where {N} = DFP_add(x,y)

```
