My question is how can I overload certain method within a certain class in Julia?
In other words suppose I have a following definition of a class:
type Sometype
prop::String
setValue::Function
# constructor
function Sometype()
this = new ()
this.prop = ""
####### v1 #######
this.setValue = function(v::Real)
println("Scalar Version was Invoked!")
# operations on scalar...
# ...
end
####### v2 #######
this.setValue = function(v::Vector{Real})
println("Vector Version was Invoked!")
# operations on vector...
# ...
end
####### v3 #######
this.setValue = function(v::Matrix{Real})
println("Matrix Version was Invoked!")
# operations on Matrix...
# ...
end
return this
end
end
So when I say in my main code:
st = Sometype()
st.setValue(val)
depending on whether val
is a scalar, vector or matrix it would invoke the corresponding version of a setvalue
method. Right now, with definition above, it overrides definitions of setvalue
with the last one (matrix version in this case).
setValue
is a variable with typeFunction
and can be overridden just like other common variables. for now, it seems julia doesn't support multiple dispatch foranonymous functions
. – RoxiesetValue
is the problem here. I want to code it in such way that it will instead compile all versions of asetValue
and one of those to get called appropriately to the type of the passed inputval
– Krusegeneric function
, try to give those functions the same name e.g.function foo(v::Real)
,function foo(v::Vector{Real})
,function foo(v::Matrix{Real})
. However, this is not julia-style, see @David's answer – Roxie