Parametric types are invariant

For the purposes of illustration, we will consider a popular type hierarchy that is used by some of the OOP literature— the animal kingdom! Everyone loves cats and dogs. I have also included crocodiles here to explain related concepts:

The corresponding code to build such a hierarchy is as follows:

abstract type Vertebrate endabstract type Mammal <: Vertebrate endabstract type Reptile <: Vertebrate endstruct Cat <: Mammal     nameendstruct Dog <: Mammal    nameendstruct Crocodile <: Reptile     nameend

For convenience, we can also define the show function for these new types:

Base.show(io::IO, cat::Cat) = print(io, "Cat ...

Get Hands-On Design Patterns and Best Practices with Julia 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.