Option 2 – Vectors of instances of Number

The second option is a little more interesting. We have switched the type parameter from Float64 to Number, which is the topmost abstract type in the numeric type hierarchy:

sumprod_2(A::Vector{Number}, B::Vector{Number}) = sum(A .* B)

Let's test it now:

At first glance, it may appear that using Number as a type parameter would make it more generic. It turns that out that it can only accept an array of Number types, which means that it has to be a heterogenous array where each element can be a different type as long as all element types are subtypes of Number. For that reason, a vector of Float64 values is not a subtype of a vector of Number values. Check the following code snippet:

For that reason, none of the scenarios was successful except the last one, which takes vectors of Number exactly as arguments. So this option is not a great one either. Let's move on!

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

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