I was reading the Pkg documentation for Julia v0.7 and I noticed that the examples use import. I was wondering if this represents idiomatic Julia and if import is recommended over using.
Having clear what using and import do, is it better to consistently use import – thinking that it doesn’t add the modules to be searched, so it provides better performance?
There is no performance difference, the difference is if the exported variables are brought into scope. using is nice for interactive code but for library code it is in my opinion more descriptive (at least when the code size starts to get larger) to use import.
Compare e.g.
using A, B, C # A exports p, B exports y, C exports z
foo(x) = p(x) + y(x) + z(x)
vs
import A, B, C
foo(x) = A.p(x) + B.y(x) + C.z(x)
or
import A.p, B.y, C.z
foo(x) = p(x) + y(x) + z(x)
In both the latter examples, it is easy to see where p, y and z comes from. In the first one you have to either run using A, B, C in the REPL and just see where they are defined, or search through the source code of A, B, and C.
Thank you very much, that’s good to know. Indeed, in library development I prefer fully qualified names – while for user facing APIs I prefer shorter names.
If I don’t need to extend methods I routinely go with using. I was considering changing that to import statements, hoping it would provide a performance gain. It’s good to know that there is none so I won’t bother doing it.