January 2020
Intermediate to advanced
532 pages
13h 31m
English
It should be quite intuitive that method arguments are covariant because that is how multiple dispatch work today. Consider the following function:
friend(m::Mammal, f::Mammal) = "$m and $f become friends."
In Julia, method arguments are formally represented as a tuple. In the preceding example, the method argument is just Tuple{Mammal,Mammal}.
When we call this function with two arguments that have type S and T respectively, then it will only be dispatched if S <: Mammal and T <: Mammal. In this case, we should be able to pass any combination of mammals—dog/dog, dog/cat, cat/dog, and cat/cat. The following screenshot proves this:
Let's also check whether a crocodile can join the party:
As expected, Tuple ...
Read now
Unlock full access