What is a dispatch?

A dispatch is the process by which a function is selected for execution. You may wonder why there is any controversy in selecting which function to execute. When we develop a function, we give it a name, some arguments, and a block of code that it should execute. If we come up with unique names for all functions in a system, then there will be no ambiguity. However, there are often times when we want to reuse the same function name and apply it to different data types for similar types of operation.

Examples are abundant in Julia's Base library. For example, the isascii function has three methods, and each one takes a different argument type:

isascii(c::Char) 
isascii(s::AbstractString)
isascii(c::AbstractChar)

Depending on the type of the argument, the proper method is dispatched and executed. When we call the isascii function with a Char object, the first method is dispatched. Likewise, when we call it with a String object, which is a subtype of AbstractString, then the second method is dispatched. Sometimes, the type of the argument being passed to the method is not known until runtime, and in that case, the proper method is dispatched right at that moment, depending on the specific value being passed. This behavior is called dynamic dispatch.

Dispatch is a key concept that will come up over and over again. It is important that we understand the rules as related to how a function being dispatched. We will go over these next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.100.34