Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple dispatch for methods of a class in Julia

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).

like image 564
aberdysh Avatar asked Sep 23 '15 00:09

aberdysh


People also ask

What is multiple dispatch in Julia?

Using all of a function's arguments to choose which method should be invoked, rather than just the first, is known as multiple dispatch.

How is multiple dispatch different from method overloading?

Method overloading is resolved at compile time. Multiple dispatch is resolved at runtime. When using double dispatch the called method depends on the actual type of receiver and arguments. Method overloading however, only allows the called method to depend on the declared type of the parameters.

What is multiple dispatch in programming?

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments.


1 Answers

This style of Object-Oriented Programming (OOP), in which the functions live inside objects, is not used in Julia.

Instead, in Julia we just define methods outside the object definition. E.g.:

type Sometype
    prop::String
end

Sometype(v::Real) = ...

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

Note that the first definition is an example of the short-hand way of defining simple functions on a single line, and the second example is for more complicated functions.

As pointed out by @GnimucKey, instead of v::Vector{Real}, you should use v::Vector{T} with the function parametrised by T. I have changed my answer accordingly. An argument specified as v::Vector{Real} will never match an argument, since it is impossible to create objects of the abstract type Real, and the invariance of types means that an object like Vector{Float64} is not a subtype of Vector{Real}.

like image 54
David P. Sanders Avatar answered Oct 12 '22 23:10

David P. Sanders