Named Modules - Maple Help

Home : Support : Online Help : Programming : Modules : Named Modules

Named Modules

 Calling Sequence module ModuleName() export eseq; local lseq; global gseq; option optseq; description desc; statseq end module

Description

 • In some circumstances, it may be necessary for module members to "know" what module they belong to. For this reason, you can create a named module, using a slightly modified syntactic form of a module definition. It differs from the ordinary syntax only in the placement of a name between the module keyword and the parentheses that follow it.
 • Evaluating a named module definition causes, as a side effect, the assignment of the resulting module to the name, and the module name is stored in the attributes of each module member.
 • Note: This causes the specified name to be protected. Thus, evaluating a global named module definition more than once will generate an error. Evaluating a local definition in separate invocations of a procedure will produce multiple named modules, each with a separate (but identical looking) protected name.
 • Named modules are also used by the system for some purposes. Every module that is saved to a repository is given a name (that under which it was saved) for use by the persistent store.
 • The name of a named module, written as a string, can also be used as the first argument to the module(...) type. This indicates that the type should only match modules that have the specified name.

Examples

 > $\mathbf{module}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{MyModule}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{_export}\left(e\right);\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}e≔2\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module}$
 ${\mathbf{module}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{MyModule}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{e}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (1)
 > $\mathrm{MyModule}:-e$
 ${2}$ (2)
 > $\mathrm{type}\left(\mathrm{MyModule},'\mathrm{protected}'\right)$
 ${\mathrm{true}}$ (3)
 > $\mathbf{module}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{aModule}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module}$
 ${\mathbf{module}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{aModule}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (4)
 > $\mathbf{module}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{aModule}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module}$

One reason for introducing named modules is so that the following distinction may be made when printing module member names in certain cases.

 > $\mathbf{module}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{NamedModule}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{_export}\left(\mathrm{e1}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module}:$
 > $\mathbf{use}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{NamedModule}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{e1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end use}$
 ${\mathrm{NamedModule}}{:-}{\mathrm{e1}}$ (5)
 > $\mathrm{UnNamedModule}≔\mathbf{module}\left(\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{_export}\left(\mathrm{e2}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end module}:$
 > $\mathbf{use}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{UnNamedModule}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{e2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end use}$
 ${\mathrm{e2}}$ (6)

Module names can be used to check that different module instances were created by some common definition.

 > make := proc(x)     local Number;     module Number() export n; end module;     Number:-n := x;     Number end proc:
 > $\mathrm{num1}≔\mathrm{make}\left(2\right)$
 ${\mathrm{num1}}{≔}{\mathbf{module}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Number}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (7)
 > $\mathrm{num2}≔\mathrm{make}\left(3.5\right)$
 ${\mathrm{num2}}{≔}{\mathbf{module}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Number}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (8)
 > $\mathrm{type}\left(\mathrm{num1},'\mathrm{module}'\left("Number"\right)\right)$
 ${\mathrm{true}}$ (9)
 > $\mathrm{type}\left(\mathrm{num2},'\mathrm{module}'\left("Number",n::'\mathrm{integer}'\right)\right)$
 ${\mathrm{false}}$ (10)