Clarification on the scope of import/using/include

Hi guys, i was trying to understand the source code of the package (conformalprediction.jl,
and i have noticed something that i didn’t expect. i am quite new to julia and
I thought that when i used import or using in a file the functions imported would be available to be called only in that specific file. However, in this package the functions defined in utils are called from every file. Moreover, despite the fact that import MLJModelInterface as MMI is only used in classifier.jl, every other file relies on the interface to define new functions.

so my question is: how does the scoping work when i use import or using?

Difference import vs using:

  • import MLJModelInterface only brings the module into scope, but none of its exported symbols. You can access all symbols from that module by prefixing them with MLJModelInterface.
  • using MLJModelInterface brings in all exported symbols of that module in the current one. You can still access non-exported symbols from MLJModelInterface as you would when you just would have imported it

import and using act on a Module basis.
If you have a module like

module ABC
include("utils.jl")
end

and

# utils.jl
import MLJModelInterface as MMI 

then MLJModelInterface will be made available also in ABC.
You can think of include(...) as ‘copy-pasting’ the code into the place where the include appeared.

1 Like