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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.