Multiple submit for class methods in Julia

My question is: how can I overload a specific method inside a specific class in Julia?

In other words, suppose I have the following class definition:

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 it is val scalar , vector or matrix , it will refer to the corresponding version of the method setvalue. Right now, with the above definition, he redefines the definitions setvaluelast (in this case, the matrix version).

+4
source share
1

- (), , .

Julia . :.

type Sometype
    prop::String
end

Sometype(v::Real) = ...

function Sometype{T}(v::Vector{T})  # parametric type
    ....
end

, , - .

@GnimucKey, v::Vector{Real} v::Vector{T} , T. . , v::Vector{Real}, , Real, , Vector{Float64} Vector{Real}.

+8

Source: https://habr.com/ru/post/1608601/


All Articles