Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `using` and `import` in Julia when building a module?

Tags:

julia

Question: What is the difference between using and import in Julia when I'm building my own module?

My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.

So, how wrong am I?

like image 704
Colin T Bowers Avatar asked Nov 23 '14 06:11

Colin T Bowers


People also ask

What is the difference between import module and from module import object?

The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.

How do you call a module in Julia?

To load a module from a package, the statement using ModuleName can be used. To load a module from a locally defined module, a dot needs to be added before the module name like using . ModuleName . would load the above code, making NiceStuff (the module name), DOG and nice available.


1 Answers

The Julia Modules documentation states:

The import keyword [...] only operates on a single name at a time. It does not add modules to be searched the way using does. import also differs from using in that functions must be imported using import to be extended with new methods. [...] Functions whose names are only visible via using cannot be extended.

(Emphasis mine.)

For example, you can use import to add methods to Base.show to pretty-print your own types, but not with using.

There is also importall that imports all exported names of a module.

(This answer refers to Julia 0.6; the documentation was reworded for 1.0.)

like image 150
user4235730 Avatar answered Oct 23 '22 10:10

user4235730