January 2020
Intermediate to advanced
532 pages
13h 31m
English
Whenever a type variable occurs more than once in a method signature, it is used to enforce the same type as determined across all positions where it occurs. Consider the following function:
add(a::Array{T,1}, x::T) where {T <: Real} = (T, a .+ x)
The add function takes an Array{T} and a value of type T. It returns a tuple of T and the result of adding the value to the array. Intuitively, we want the type T to be consistent across both arguments. In other words, we would want the function to be specialized in each of the realizations of T when the function is called. Obviously, the function works great when the type agrees:
In the first case, T is determined to be Int64, and in the second case, T is determined to ...
Read now
Unlock full access