A multiple-dispatch example

Let's now explore an example about people working in a company to show multiple dispatch in action. Let's define an abstract type Employee and a type Developer that is a subtype:

abstract type Employeeendmutable struct Developer <: Employee    name::String    iq    favorite_lang::Stringend

We cannot make objects from an abstract type: calling Employee() only returns an ERROR: MethodError: no constructors have been defined for Employee error message.

The type Developer has two implicit constructors, but we can define another outer constructor, which uses a default constructor as follows:

Developer(name, iq) = Developer(name, iq, "Java")

Outer constructors provide additional convenient methods to construct objects. Now, we ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.