The availability of type variables

An important feature of the parametric method is that the type variable specified in the where clause is also accessible from the method body. Contrary to what you might think, this is not always true. Here, we will present a case where the type variable is not available at runtime.

Consider the following functions:

mytypes1(a::Array{T,1}, x::S) where {S <: Number, T <: S} = T
mytypes2(a::Array{T,1}, x::S) where {S <: Number, T <: S} = S

We can use the mytypes1 and mytypes2 functions to experiment with what type variables are derived by the Julia runtime. Let's start with the happy case:

However, the picture is not always rosy. In other situations, it may not work 100% of the time. Here's an example:

Why is S not defined here? First of all, we already know that T is Signed because the parametric type is invariant. As part of the where clause, we also know that T is a subtype of S. As such, S could be Integer, Real, Number, or even Any. As there are too many possible answers, the Julia runtime decided not to assign any value to S.

The moral of the story is don't assume that a type variable is always defined and accessible from the method, especially for a more complex situation like this. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.125