Piracy anti-pattern

In Chapter 2, Modules, Packages and Data Type Concepts, we learned how to create new namespaces using modules. As you may recall, modules are used to define functions so that they are logically separated. It is possible, then, that we can define two different functions—one in module X and another in module Y, with both having exactly the same name. In fact, these functions do not even need to mean the same thing. For example, in a mathematics package, we can define a trace function for matrices. In a computer graphics package, we can define a trace function for doing ray tracing work. These two trace functions perform different things, and they do not interfere with each other. 

On the other hand, a function can also be designed to be extended from another package. For example, in the Base package, the AbstractArray interface is designed to be extended. Here's an example:

# My own array-like type for tracking scores
struct Scores <: AbstractVector{Float64}
values::Vector{Float64}
end

# implement AbstractArray interface
Base.size(s::Scores) = (length(s.values),)
Base.getindex(s::Scores, i::Int) = s.values[i]

Here, we have extended the size and getindex functions from the Base package so that they can work with our own data types. This is a perfectly good usage of the Julia language; however, it can be problematic when we do not extend functions from other packages correctly. In particular, piracy refers to the situation in which a third-party function is replaced or extended in a bad way. This is an anti-pattern because it may cause system behavior to become nondeterministic. For convenience, we can define three different kinds of piracy:

  • Type I piracy: Function is redefined 
  • Type II piracy: Function is extended without using your own types in any of the arguments 
  • Type III piracy: Function is extended but used for a different purpose

We will now drill down into each one in more detail.

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

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