Julia style: import vs using

In the QuantEcon lectures we typically bring all names into the top level namespace via using ModuleName. See, e.g., here.

I feel more comfortable with import ModuleName. For example, import PyPlot or import PyPlot: plot, since it doesn’t crowd the global namespace. On the other hand, I recognize that this is less of an issue in Julia (because of multiple dispatch) and I don’t want to break with standard style. Any thoughts?

cc @spencer.lyon @cc7768

I don’t have particularly strong opinions on this per se… It seems like most people use the using PackageName syntax, but the Pythonista in me understands your worry about crowding the global namespace (though I think multiple dispatch makes it less likely for us to have issues).

I don’t think we want to using though – It seems like from this page we would want to use something like using PackageName: FunctionName1, FunctionName2 instead of import PackageName.FunctionName because import only seems to brings a single function at a time in whereas the using syntax can bring multiple functions in. The import syntax is necessary though if we would like to add additional versions of a function.

I think the strong convention in Julia code is to use using, but there have been really smart people in Julia’s core community that don’t like the implicit dump of names into the current scope.

What I usually do is call using PackageName from scripts or examples and using PackageName: func1, Type2 from libraries (unless I need to import many names from PackageName, in which case I revert to the former). You can see that a little bit in QuantEcon.jl

RE @cc7768 comment. import can bring in more than one name at a time. The difference is that when you use import PackageName: func1 you can add new methods to func1 without prefacing the func1 with PackageName. e.g. this will just work:

import Base: rand

rand(x::MarkovChain) = draw(x)

whereas if using was used we’d have to do

using Base: rand # assuming  this is some function not imported by default

Base.Rand(x::MarkovChain) = draw(x)

In the style guideline for the lectures updates we are using using Pkg in general. I personally prefer to call using PkgName: a, b, c to know what exactly I am taking, but we want to make the lectures fool-proof and thus tested against using Pkg. import should only be used when actually modifying the namespace. In actual development, I use

using PkgName:  A, a
using PkgName2: B, c

import PkgName: C