January 2020
Intermediate to advanced
532 pages
13h 31m
English
As we learned earlier, it is a nice feature to be able to match type variables and keep them consistent across method arguments. In practice, there are situations where we want to be even more specific when determining the right type for each type variable.
Consider this function:
diagonal(x::T, y::T) where {T <: Number} = T
The diagonal function takes two arguments with the same type, where the type T must be a subtype of Number. The type variable T is simply returned to the caller.
When T is concrete, it is easy to reason that the types are consistent. For example, we can pass a pair of Int64 values or a pair of Float64 values to the function and expect to see the respective concrete type returned:
Intuitively, ...
Read now
Unlock full access