"using" keyword in Julia

I’m a bit confused about the “using” keyword in Julia.

using OpenStreetMapX
distance1(a::ECEF, b::ECEF) =....

The above code works. On the other hand, if inside of a module, say module myModule,

module myModule
using OpenStreetMapX
......
end

then

using myModule
distance1(a::ECEF, b::ECEF) =....

does not work. Apparently, the ECEF is not recognisable. This is rather strange from my understanding.

1 Like

You are asking a very general question, with a very specific reference to a particular package, yet without any runable code, or what “does not work” means in this context. This makes it hard to help you.

That said, I am guessing that you want to define a method for OpenStreetMapX.distance. Either import it, or qualify with the package name. Please read
https://docs.julialang.org/en/v1/manual/modules/

Also, you seem to be asking a lot of general Julia questions here (which is fine), but all somehow involving OpenStreetMapX. Even if your work focuses on this specific package, I would suggest that you consider reading the general Julia docs multiple times carefully, so that you gain a better understanding about the language. I think it would help you in the long run.

Finally, please consider reading

1 Like

Thank you.

Maybe you are after this functionality:

1 Like
using myModule
distance1(a::ECEF, b::ECEF) =....

When I run the above code, it gives me “UndefVarError: ECEF not defined”. It’s a bit strange. Because I have “using myModule” in the same file, and inside of the module “myModule.jl”, I have “using OpenStreetMapX”.

Just implement Reexport ( as KajWiik mentioned)

module myModule
 using Reexport
 @reexport using OpenStreetMapX
......
end
1 Like

Yes, but that using OpenStreetMapX brings the exported symbols of the latter into the namespace of myModule. They are available as eg myModule.ECEF, but since they are not exported to Main, you don’t see them there.

Personally I find something like

using OpenStreetMapX, myModule

conceptually cleaner than reexporting things.

4 Likes

So an better solution when only ECEF is needed outside the module, could be

module myModule
 using OpenStreetMapX
 export ECEF
......
end
1 Like

I did this earlier, but then I deleted it from the module “myModule.jl”. Thank you a lot.

Perhaps you missed what I suggested above: simply using OpenStreetMapX in Main, too.

I don’t think that reexporting is a better solution, except possibly in a user-facing umbrella package that uses other modules/packages as internal implementation details (but that does not seem to be the case here, OpenStreetMapX is a package designed for direct use).

Fine-grained control over namespaces may seem tedious at first, but it is generally valuable as it is very easy to clutter your namespace and get conflicts even in medium-sized projects.

3 Likes

Ok the point is yours…

1 Like