Three File Scope Example

Three File Example

To the interpreter any one file looks like any other file and file extensions do not matter. The interpreter will execute a "main" file (with .ex extension) the same way it executes a "module" file (with .e extension). When one file includes another you set up a primary:module relationship between those two files; this relationship is important because you must understand which identifiers are visible to the primary file. Modules can contain other modules up to thirty (30) levels. Fortunately you can understand what happens by looking at a pair of files at a time.

Consider this system of three files. The main file includes two modules (one module containing another module) with its single include statement:

http://euphoria.derekparnell.id.au/triscope/triscope-1.png

Start by looking at some_lib.e which includes sublib.e :

http://euphoria.derekparnell.id.au/triscope/triscope-2.png

When you launch eui some_lib.e it executes as a main "primary" source-code file (the extension .e does not matter). The included sublib.e is treated as a "module" file. All statements execute as expected. This works because the export procedure bar() statement exposes the procedure bar() to the primary file. In the primary file bar() is visible and it executes.

When you launch eui my_app.ex it is treated as the main "primary" file and some_lib.e is treated as an included "module" file. An include statement, without a public scope modifier, is just an ordinary invisible statement to a primary file. The identifiers from the nested sublib.e file are invisible to the primary file. The program crashes with an error statement:

http://euphoria.derekparnell.id.au/triscope/triscope-3.png

The public scope modifier makes an include statement visible to an including file. A public include is special because it not only exposes public identifiers to a primary file, but it also exposes public identifiers to the entire chain of nested public include statements. A public include will pass on any public identifiers to a single (unmodified) include statement, and to all public include statements that are part of a nested module system.

http://euphoria.derekparnell.id.au/triscope/triscope-4.png

The main primary file only needs an unmodified include statement to see all all public identifiers. Only the nested modules themselves need public scope modifiers. This is how the Euphoria standard library is designed.

The three file example is now re-written so it executes as intended:

http://euphoria.derekparnell.id.au/triscope/triscope-5.png

To summarize this example:

  • export scope modifier exposes identifiers only to the immediate including file.
  • public scope modifier exposes identifiers to a chain of public include files.

Note: The global scope modifier would have worked in this example. A global identifier is visible to all files. We say that a global identifier "pollutes" the scope of the entire program; using global as a scope modifier is considered to be bad programming style.

Search



Quick Links

User menu

Not signed in.

Misc Menu