Julia- Warning Method definition is overwritten

I the following warnings when i execute the code written below

WARNING: Method definition (::Type{Main.Vector2D})(Real, Real) in
module Main at /…/juliaIntro.jl:13
overwritten at /…/juliaIntro.jl:13.
WARNING: Method definition (::Type{Main.Vector2D})(Any, Any) in module
Main at /…/juliaIntro.jl:13 overwritten
at /…/pythonlab/juliaIntro.jl:13.

And these warning are not consistent like it doesn’t show up for every execution, it randomly shows up. Sometimes the code just executes fine without any warning. Any pointers on why these warning are showing up. I am running Julia Version 0.5.1 on Mac 10.12.4

import Base.+

+(s1::String, s2::String) = string(s1, " ",s2)
println("Hello" + "World")

+(s1:: String, s2::Number) = s1 + " $s2"
+(s1:: Number, s2::String) = "$s1 " + s2
println("Hello World" + 46)
println(46 + "Hello World" + 46)

immutable Vector2D
  x::Real
  y::Real
end

v = Vector2D(3,5)
w = Vector2D(9.0,8.0)

println(v)
println(w)

This will happen if you re-evaluate this file in v0.5.x in the same Julia process since the methods will be be re-written. This is fixed in v0.6.

You probably want

immutable Vector2D{T1,T2}
  x::T1
  y::T2
end

or match the types:

immutable Vector2D{T}
  x::T
  y::T
end

Using a type parameter is good for performance. Using abstract types for fields is bad for performance.

1 Like

Thank You very much Chris. And also thanks for the tip on the performance front.