# Redefinition of + with custom types delivers errors

**URL:** https://discourse.julialang.org/t/redefinition-of-with-custom-types-delivers-errors/1466
**Category:** General Usage
**Created:** [January 13, 2017, 4:32pm UTC](https://discourse.julialang.org/t/redefinition-of-with-custom-types-delivers-errors/1466 "2017-01-13T16:32:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [January 13, 2017, 4:32pm UTC](https://discourse.julialang.org/t/redefinition-of-with-custom-types-delivers-errors/1466/1 "2017-01-13T16:32:46Z")

</div>

Hi folks,

I was trying to implement my own add function for my own data types but that seems to deliver errors that I can’t find the reason for. Just look at this example

```julia
type mine
    x :: Int8
    z :: Float64
    mine() = new()
end

function ++(U::mine, V::mine)
    w = mine()
    w.x = U.x + V.x
    w.z = U.z + V.z
    return w
end;

# define two instances of mine()
u = mine()
u.x = 1
u.z = 3.1416
v = mine()
v.x = 2
v.z = 2.7183

# add them up
u ++ v

> mine(3,5.8599)

# We succeeded :)

# Now with standard + instead of ++
# It's the exact same code with ++ replaced with + !

function +(U::mine, V::mine)
    w = mine()
    w.x = U.x + V.x
    w.z = U.z + V.z
    return w
end;

```

> LoadError: error in method definition: function Base.+ must be explicitly imported to be extended  
> while loading In[5], in expression starting on line 1

…so adding a (working) new method for + yields an error and it can’t be used.

So what am I doing wrong? Any help will be greatly appreciated 🙂

Best regards,

Ferran.

---

<div class="post-metadata">

### Author: ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)
#### Post date: [January 13, 2017, 4:38pm UTC](https://discourse.julialang.org/t/redefinition-of-with-custom-types-delivers-errors/1466/2 "2017-01-13T16:38:09Z")

</div>

Basically, if you want to extend a method defined in other module, you need to explicitly import or specify the module name when defining a new method. So, the following code will work as you expect:

```julia
type Mine
    x::Int8
    y::Float64
end

function Base.:+(a::Mine, b::Mine)
    return Mine(a.x + b.x, a.y + b.y)
end

#=
julia> Mine(0, 0) + Mine(1, 1)
Mine(1,1.0)

=#

```

EDIT

Another way is explicitly importing a method you want to extend from its module before defining a new method:

```julia
import Base: +

function +(a::Mine, b::Mine)
    return Mine(a.x + b.x, a.y + b.y)
end

```
