Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function Base.+ must be explicitly imported to be extended

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

like image 225
MJ Sameri Avatar asked Mar 19 '17 16:03

MJ Sameri


1 Answers

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)
like image 70
David P. Sanders Avatar answered Oct 13 '22 23:10

David P. Sanders