Modules
Modules are a way of introducing named programming elements into a Glich script. These elements can be objects, functions, commands, etc. It does this in one of two ways:-
- By immediately reading in the module's script.
- By introducing the module's contents so that it is only read when they are referenced.
| Statement module Format |
|---|
| module system:name; |
| module system:name { object obj_name1 obj_name2 ...; }; |
In the core language, the module block can only contain object definitions. Language extensions (such as the Hics extension) allow other definitions to be included.
Module Signature: system:name
A module is identified by its signature which consists of two parts:-
- system - This indicates the module system. The special system name file indicates that the module is to be read from a script file. Other names are used for internally defined modules.
- name - This is the module name. When using the file system, this is the file name (with optional path).
The signature is a text string with no spaces, but if it conform to the rules for a code name, it can be used without quotes.
A code name can contain letters, digits, the colon and underscore characters. It cannot start with a digit and must not contain spaces.
Statement: module (Read Immediately)
A module statement can be used to immediately read in the module's script. This is useful when the module contains code that must be run to set up the environment for the rest of the script. An example of this would be a math module that defines mathematical functions.
| module Statement | |
|---|---|
| Example | Output |
| // Script file: games/simon.glcs command simon(action) { write "Simon says: " + @quote(action) nl; }; | |
| // Script file: test_simon.glcs module "file:games/simon"; call simon("Jump around!"); | Simon says: "Jump around!" |
Statement: module (Introduce Contents)
A common use case for Glich is to read in a library of statements ready for for use by the host program. The use of modules is intended to reduce the need to read in all of the statements. A module statement will introduce the object's code name so that the actual module is only read if it is required.
| module Statement | |
|---|---|
| Example | Output |
| // Script file: test_mod.glcs object test_sum { values one two three; function sum { result = one + two + three; }; }; | |
| // Script file: test_run.glcs module file:test_mod { object test_sum; }; let ts = {test_sum 1, 2, 3}; write ts nl ts@sum; | {test_sum 1, 2, 3} 6 |