As the title say, i am wandering if it is OK to reexport a method from a different package. I currently have :
module MyPackage
using OtherPackage
struct mytype end
OtherPackage.function(x::mytype) = 1
end
The fact is that MyPackage provides new methods to this function, on types that are defined in MyPackage (so no type piracy), but the generic is only exported if I do :
using MyPackage
# Cannot use `function`
using OtherPackage
# Now i can
Would it me OK to export OtherPackage.function, so that i could do:
Thanks. Do you know if there is a âstandardâ, or a good practice regarding the choice between re-exporting and telling people to load both packages ?
I would say that it depends on the relationship between your package and the other package. If your package builds âon top ofâ the other package but a user wouldnât particularly need to be aware of the underlying package then re-export. If your package is an addition that a user would be using while also using the underlying package directly then advise they load both.
What is the exact syntax for re-exporting a function from another package?
This is not working for me.
EDIT: Sorry, this was a git and a dev vs. add issue. The code below is now working as intended.
module MyPackage
using AlgebraOfGraphics
export draw
struct MyType
data::SomeOtherType
plot::Layer # type from AoG
end
export MyType
function f()
# computations
return MyType(data, plot)
end
export f
end # module
julia> using MyPackage
julia> results = f();
julia> draw(results.plot)
I did get an unexpected error when I wrote export AlgebraOfGraphics.draw instead of export draw though. I would have thought either would work?
julia> using MyPackage
[ Info: Precompiling MyPackage [d29f79e6-4313-439e-a1e0-c6c95527bf41]
ERROR: LoadError: syntax: "module" at <file_path>.jl:1 expected "end", got "."
If you have package B and C which extend a method from package A, is it okay if both B and C export the extended function from A? Any best practices to be aware of?
Iâm in a similare situation, where this time OtherPackage is LinearAlgebra. In my package QuantumToolbox.jl I apply several methods of the LinearAlgebra.jl functions to my types.
What should I do? I it a good idea to reexport LinearAlgebra?
I was currently aware of Reexport.jl. My question was more related to the choice or not to re-export the LinearAlgebra package.
I see that many people just re-export their dependencies belonging to the same ecosystem. So I was wondering if re-exporting a Base library like LinearAlgebra was something to avoid or not.
I donât think anything âbadâ would happen, but, personally, I would definitely frown upon in. If you donât âownâ it, it seems to me you shouldnât export it. (âOwnâ can mean âin my orgâ, so re-exporting from a base package is fine).
But then, Iâm fairly conservative when it comes to exports, anyway. I might even lean towards considering it a bad idea altogether (it pushes people towards âstar importsâ, i.e., unqualified using, which took the Python community years to root out after finding out that it was a really bad idea). Maybe now that we have public, packages shouldnât declare exports at all anymore. But in any case, I think of export as âpublic plus convenience in the REPL for throwaway codeâ. If you think in terms of public, it feels even weirder to declare something public that you donât own. So, the same should apply to export. But I donât think thereâs a âcorrectâ answer here. This is completely opinion based, so at best, youâll get a community majority opinion poll on how people feel about exports, which will probably be divided between the types of people that I might call âusersâ and âdevelopersâ
In your case, is there any concrete benefit to users not having to do an explicit using LinerAlgebra? I believe the mantra âexplicit is better than implicitâ holds.