Dodo doc > Source Files > Use of Modules


Use of modules

Importing symbols from a module

The "use" command pulls the declarations of a module in the current location. It is as if the symbols were declared here but they are actually delegated to the module.

For example:

__Module__ a (version: "1")
__Header__

def c = "hello"

__Module__ b (version: "1")
__Main__

use a
def k = c.chars  # c is declared, same as a.c

The command can also be used to pull the members of an object into a type, delegating them to that object.

Since "use" pulls all the declarations it is not recommended to use it on an external module. Instead it should be used on a library which typically exports some modules, or on a service which exports the capabilities required to use that service.

To pull a single symbol just redeclare it locally.

For example:

def c = a.c   # c is declared, same as a.c
Rationale: when importing modules from a library instead of symbols, the declared symbols become one level deep which limits the bloating of the current namespace. Redeclaring a symbol from a module gives an opportunity to rename it in line with your own naming conventions.

Compile time imports

The compiler can receive a list of module, library or service files which can be precompiled or not. Any module, library or service in these files is added to the predefined modules library.

That is the reason why dodo files often start with:

use modules
Rationale: the predefined modules library makes it easy for dodo code to access the symbols it needs. Passing a list to the compiler is a flexible method, for example mock libraries can be passed to the compiler for unit tests.

Run time imports

There is a load service which provides the capability to load a precompiled module, library or service dynamically. A dynamic module, library or service does not get added to modules so it is often made public to enable other files to use it.

The load service relies on rules described in a version.info file to select the appropriate source for each required module, library or service.

Rationale: loading libraries dynamically allows to share them between applications which frees some disk and some memory. The version.info file allows to adjust the loader rules to a particular environment.