Type I – Redefining a function

Type I piracy refers to the situation where a programmer redefines a third-party function from their own module. Perhaps you did not like the original implementation in the third-party module and replaced the function with your own implementation.

The worst form of Type I piracy is when you replace the function without conforming to the original function's interface. Let's do an experiment and see what could happen. We will use the + function from Base as an example. As you know, when the + function is passed with two Int arguments, it should return an Int value as a result. What would happen if we replace the function so that it returns a string? Let's open up a REPL and give it a try:

Boom! The Julia REPL crashed immediately as soon as the function was defined. That is because the return value of this + function is expected to be an integer. When we return a string, it violates the contract of this function and all functionalities that rely on the + function are negatively impacted. Given that + is a commonly used function, it crashes the system immediately.

Why does Julia even allow us to do that? For some situations, the ability to do this can be useful. Say that you found a bug for a specific function in a third-party package—you can inject a fix immediately without having to wait for the bug fix upstream. Likewise, you can replace a slow function with a more performant version. Ideally, these changes should be sent upstream, but you have the flexibility to implement the change immediately.

The only requirement is that the function being replaced should adhere to the same contract that was originally intended. Therefore, it requires an intimate understanding about how the third-party package is designed. In reality, it is even better if you can contact the original author and discuss the change before applying piracy.

With great power comes great responsibility. Great care must be taken if we ever want to make use of Type I piracy. 

Next, we will look into Type II piracy, which is more common across packages in the Julia ecosystem.

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

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