11
11
can control which names from other modules are visible (via importing),
12
12
and specify which of your names are intended to be public (via exporting).
14
The following example illustrates the major features of modules::
14
The following example demonstrates the major features of modules. It is
15
not meant to be run, but is shown for illustrative purposes::
29
30
show(io, a::MyType) = print(io, "MyType $(a.x)")
32
Note that the style is not
33
to indent the body of the module, since that would typically lead to
34
whole files being indented.
36
This module defines a type ``MyType``, and two functions. Function ``foo``
37
and type ``MyType`` are
38
exported, and so will be available for importing into other modules.
39
Function ``bar`` is private to ``MyModule``.
41
The statement ``using Lib`` means that a module called ``Lib``
42
will be available for resolving names
43
as needed. When a global variable is encountered that has no definition in
44
the current module, the system will search for it in ``Lib`` and import it
33
Note that the style is not to indent the body of the module, since
34
that would typically lead to whole files being indented.
36
This module defines a type ``MyType``, and two functions. Function
37
``foo`` and type ``MyType`` are exported, and so will be available for
38
importing into other modules. Function ``bar`` is private to
41
The statement ``using Lib`` means that a module called ``Lib`` will be
42
available for resolving names as needed. When a global variable is
43
encountered that has no definition in the current module, the system
44
will search for it in ``Lib`` and import it if it is found there.
46
45
This means that all uses of that global within the current module will
47
46
resolve to the definition of that variable in ``Lib``.
93
92
There are three important standard modules: Main, Core, and Base.
95
94
Main is the top-level module, and Julia starts with Main set as the
97
Variables defined at the prompt go in Main, and ``whos()`` lists variables
95
current module. Variables defined at the prompt go in Main, and
96
``whos()`` lists variables in Main.
100
98
Core contains all identifiers considered "built in" to the language, i.e.
101
99
part of the core language and not libraries. Every module implicitly
116
114
If these definitions are not wanted, modules can be defined using the
117
115
keyword ``baremodule`` instead. In terms of ``baremodule``, a standard
118
``module`` looks like this:
116
``module`` looks like this::
132
130
it is not exported. This is often useful when debugging.
134
132
Macros must be exported if they are intended to be used outside their
136
Macro names are written with ``@`` in import and export statements, e.g.
133
defining module. Macro names are written with ``@`` in import and
134
export statements, e.g. ``import Mod.@mac``.
139
136
The syntax ``M.x = y`` does not work to assign a global in another module;
140
137
global assignment is always module-local.