8

i'm pretty new to julia forgive me if my question is dumb,

for exmaple i defined a type like this:

type Vector2D
    x::Float64
    y::Float64
end

and 2 object w and v:

v = Vector2D(3, 4)
w = Vector2D(5, 6)

if i add them up it will raise this err : MethodError: no method matching +(::Vector2D, ::Vector2D) it's ok , but when i want to define a method for summing theses object

+(a::Vector2D, b::Vector2D) = Vector2D(a.x+b.x, a.y+b.y)

it raise this error :

error in method definition: function Base.+ must be explicitly imported to be extended

julia version 0.5

Javad Sameri
  • 1,218
  • 3
  • 17
  • 30

1 Answers1

11

As the error message says, you must tell Julia that you want to extend the + function from Base (the standard library):

import Base: +, -

+(a::Vector2D, b::Vector2D) = Vector2D(a.x + b.x, a.y + b.y)
-(a::Vector2D, b::Vector2D) = Vector2D(a.x - b.x, a.y - b.y)
David P. Sanders
  • 5,210
  • 1
  • 23
  • 23