Trouble exporting functions from different packages with different methods but same name

Hi!

I have two different packages. Both export the same function getLandSea, but the methods are different. These two packages sometimes intersect, because they are climate-related datasets, but are for different datasets so they should be separate packages.

However, I am unable to export these functions. Instead, I must use ERA5Reanalysis.getLandSea or NASAPrecipitation.getLandSea to use these functions. Is there are way to get around this?

Best
Nat

1 Like

One option is import ... as:

julia> module A
         f() = "A"
         export f
       end
Main.A

julia> module B
         f() = "A"
         export f
       end
Main.B

julia> using ..A, ..B

julia> import ..A.f as fA

julia> import ..B.f as fB

julia> fA()
"A"

As I understand it, one common way to solve this is to have a ...Base package (something like ClimateDatasetBase.jl) where you declare

function getLandSea end

and then have

import ClimateDatasetBase: getLandSea
function getLandSea(...)
 ...
end

in both your packages, to make them actually be methods of the same function.

Whether this is worth doing of course depends on how many such functions you have, but such a base package also (1) lets you define more of a common interface between the packages, (2) is a place to define Abstract... types for the child packages to subtype, and (3) can make it easier for others writing a climate dataset-handling package to integrate better with your existing ones.

1 Like