Option 7 – Duck typing

Our last option basically skips the types in the function arguments. This is also called duck typing, as the function will be dispatched as long as two arguments are provided. Julia will specialize and compile a new version for different variations of the argument types. The function is simply defined as follows:

sumprod_7(A, B) = sum(A .* B)

For completeness, we will run the test again:

The benefit of this option is that the function is free of type information in the signature, so it looks very clean. However, the drawback is that the function can be dispatched for any type—not even an array or for numeric values. When garbage is passed into the function, garbage comes out, or the function just throws an error when the objects being passed do not have the * operator function defined.

Now that we have considered all the options and performed the respective tests, let's summarize what we have done so far and what we would want to do next.

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

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