|
| 1 | +# Type piracy |
| 2 | + |
| 3 | +Type piracy is a term used to describe adding methods to a foreign function |
| 4 | +with only foreign arguments. |
| 5 | +This is considered bad practice because it can cause unexpected behavior |
| 6 | +when the function is called, in particular, it can change the behavior of |
| 7 | +one of your dependencies depending on if your package is loaded or not. |
| 8 | +This makes it hard to reason about the behavior of your code, and may |
| 9 | +introduce bugs that are hard to track down. |
| 10 | + |
| 11 | +See [Julia documentation](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) for more information about type piracy. |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | +Say that `PkgA` is foreign, and let's look at the different ways that `PkgB` extends its function `bar`. |
| 16 | + |
| 17 | +```julia |
| 18 | +module PkgA |
| 19 | + struct C end |
| 20 | + bar(x::C) = 42 |
| 21 | + bar(x::Vector) = 43 |
| 22 | +end |
| 23 | + |
| 24 | +module PkgB |
| 25 | + import PkgA: bar, C |
| 26 | + struct D end |
| 27 | + bar(x::C) = 1 |
| 28 | + bar(xs::D...) = 2 |
| 29 | + bar(x::Vector{<:D}) = 3 |
| 30 | + bar(x::Vector{D}) = 4 # slightly bad (may cause invalidations) |
| 31 | + bar(x::Union{C,D}) = 5 # slightly bad (a change in PkgA may turn it into piracy) |
| 32 | + # (for example changing bar(x::C) = 1 to bar(x::Union{C,Int}) = 1) |
| 33 | +end |
| 34 | +``` |
| 35 | + |
| 36 | +The following cases are enumerated by the return values in the example above: |
| 37 | +1. This is the worst case of type piracy. The value of `bar(C())` can be |
| 38 | + either `1` or `42` and will depend on whether `PkgB` is loaded or not. |
| 39 | +2. This is also a bad case of type piracy. `bar()` throws a `MethodError` with |
| 40 | + only `PkgA` available, and returns `2` with `PkgB` loaded. `PkgA` may add |
| 41 | + a method for `bar()` that takes no arguments in the future, and then this |
| 42 | + is equivalent to case 1. |
| 43 | +3. This is a moderately bad case of type piracy. `bar(Union{}[])` returns `3` |
| 44 | + when `PkgB` is loaded, and `43` when `PkgB` is not loaded, although neither |
| 45 | + of the occurring types are defined in `PkgB`. This case is not as bad as |
| 46 | + cases 1 and 2, because it is only about behavior around `Union{}`, which has |
| 47 | + no instances. |
| 48 | +4. Depending on ones understanding of type piracy, this could be considered piracy |
| 49 | + as well. In particular, this may cause invalidations. |
| 50 | +5. This is a slightly bad case of type piracy. In the current form, `bar(C())` |
| 51 | + returns `42` as the dispatch on `Union{C,D}` is less specific. However, a |
| 52 | + future change in `PkgA` may change this behavior, e.g. by changing `bar(x::C)` |
| 53 | + to `bar(x::Union{C,Int})` the call `bar(C())` would become ambiguous. |
| 54 | + |
| 55 | +!!! note |
| 56 | + The test function below currently only checks for cases 1 and 2. |
| 57 | + |
| 58 | +## [Test function](@id test_piracies) |
| 59 | + |
| 60 | +```@docs |
| 61 | +Aqua.test_piracies |
| 62 | +``` |
0 commit comments