Multiple dispatch in Julia language

Asked

Viewed 90 times

4

I’m studying about the language Julia and read that the multiple dispatch allows the functions to be dispatched dynamically, but it’s still not clear to me.

1 answer

2


TL; DR

It’s just one overload of functions resolved at runtime. If you know the types of objects at compile time do not need this mechanism (provided that the language allows solving in the compilation).

Detailing

First point to define is that this type of mechanism only works in language that have some dynamic form of execution, so the decision about the call of a function is given only nom moment to be called and not at the time of compilation as usually happens in languages that tend to solve everything more statically.

The more static languages usually make the dispatch based on information that is available in the compilation, so we have polymorphism ad hoc (or overload) or parametric.

Even the most static languages often have some dynamically ordered function resolution mechanism, which is known as classical polymorphism or subtype. Depending on the object treated it is decided which method to call. It uses only one criterion to make the decision, it is the type of an argument (it is usually the first that can even be hidden in the code). It is a relatively simple mechanism and the cost of Runtime of it is very low.

The multiple dispatch uses several criteria to decide what to call. It can analyze the types of various arguments, past values or even other information that help in the decision. It closely resembles the overload of functions, but differs because the decision depends on the timing of the execution, and gives more flexibility.

It is a more sophisticated form of polymorphism.

Some languages that do not have this type of mechanism end up solving in the code what to do, with ifs or some form of switch including Pattern matching. The Multiple Dispatch does it on its own without needing that kind of code.

A solution to partially resolve this issue is to implement the Visitor Pattern. Some cases with code generation to facilitate.

There are others that end up doing more or less the same thing that a compiler would do internally in the code of languages that natively support this pattern, as happens in Julia.

Some languages create a way for you to write the various functions with possible combinations of different features of the arguments.

Languages that don’t do this and you don’t use a code generator end up generating a lot Boiler Plate, and the ones that do are good facilitators for this, although it generates an extra processing cost.

In Julia you can have it:

function collide(me::Circle, other::Rectangle)
function collide(me::Polygon, other::Circle)
function collide(me::Polygon, other::Rectangle)

And then you call it:

collide(objeto, outro)

What will be called depends on the type of these two variables at the time of execution, it is not known in advance and the decision will be made by Runtime without you having to write any code to make that decision.

So if objeto be the type Circle and outro be the type Rectangle will be the first function to be called. But if outro for Circle and it doesn’t matter which Polygon be the objeto, a segunda função será chamada, e finalmente se o anotherfor umRectangleeobject is any Polygon, except a Circle who already has a match better, then the third function will be called. Note that it will error if both are a Polygon that the outro not be Circle or Rectangle.

There is something called double Dispatch which is a specialisation of Multiple Dispatch for two characteristics that is different from the single Dispatch which has only one and is used more widely. It is what was used above.

They are also called multi-methods.

Documentation. Wikipedia.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.