"Under the hood" difference between import and using

deaminase
Mon, 11 Sep 2023 14:24:35 +0000
Internals & Design

So, based on the docs I know that eg. import doesn’t bring in any exported identifiers to your namespace when importing a module, and if you want to add a method to a function without using its fully qualified name, you have to do eg. import Base: show before defining function show(io, x::MyType) …, but I’d like to know what the difference between using and import really is under the hood.

Base docs say this for using:

using Foo will load the module or package Foo and make its exported names available for direct use.

and this for import:

import Foo will load the module or package Foo . Names from the imported Foo module can be accessed with dot syntax (e.g. Foo.foo to access the name foo ). See the manual section about modules for details.

For modules the difference between using and import is pretty clear, but using Foo: corge and import Foo: corge seem to do the same thing except that you can’t add methods to a function that was brought into the namespace with using, so this left me wondering what the difference of those two forms is from the compiler’s / runtime’s standpoints

10 posts - 4 participants