Scope Export Example

Simple Example

In this example there are two files. The main file is myprog.ex and the module file is inc.e ; from the file extensions you can recognize that the .ex file will include the .e file.

The include statement works "as if" the module file is pasted into the primary file; the result is one big file to the interpreter. If this were completely true then few programs would run correctly. You can be sure that some identifiers from the module file will clash with identifiers already in use by the primary file. Since modules can include other modules you can imagine a program that becomes infinite in size.

The default is that when you include a module:

  • a module is only included once (repeated include statements are ignored).
  • statements in a module execute the first time they are encountered.
  • all declarations are invisible (no identifiers from the module can be used by the primary file).

The examples are color coded to show:

  • black...ordinary statements.
  • blue...statements execute when included.
  • gray...statements are invisible when included.
  • comments in red.

The module declaration atom x = 1 is invisible to the primary file, the include inc.e statement does not introduce the variable x , and the statement ? x results in an error. The interpreter recognizes the syntax error before it can execute the puts statement.

Use the export keyword to make a declaration visible to the primary including file:

Now there are no syntax errors. The puts statement first displays hello . The export keyword allows the declaration of x to be visible outside of the module, the include statement introduces the variable x in the primary file, and the value 1 is output.


Quick Links

User menu

Not signed in.

Misc Menu