Is it OK to structure my packages like this (benign type piracy)?

Type piracy means defining methods on functions you don’t own for arguments whose types you don’t own.

I would consider “you” to actually mean “you”/“your organization”, not necessarily “your particular package”. So, if you own PackageA and PackageB, then what you’re proposing should be okay (although it may not be the best design). This is probably a pretty common pattern between a user-facing package and a “base” package.

I would point out that

"""
     a::Function

Only add methods if you're `PackageB`!
"""

isn’t right: Anyone can and should add methods to a as long as they own any of the types of the arguments they want to pass to a. But you probably just mean “don’t add methods here”.

If you don’t “own” both PackageA and PackageB, then this approach is type piracy, and you should absolutely not do it. In particular, if PackageA and PackageB are not tightly coupled and coordinated, then it is the responsibility of PackageA to define methods for a for any Base types, or any types defined in PackageA.

5 Likes