Introduction
In the Iguana Translator variables are initialized at different times depending on where they are declared.
In the main module variables declared outside the main() function are initialized once when the channel is started. Variables declared within the main() function are initialized every time a message is processed by the Translator.
Note: The same principle applies to other modules that are “required” in the code. Variables declared within a module but outside a function (i.e., variables with module scope) are initialized once when the channel starts, and variables within functions are initialized whenever that function is called.
Tip: It is a recommended Best Practice Name “constant” values using ALLCAPS to indicate that they are CONST type values and should not be changed. However please be aware that this just a convention as there is no static CONST type in Lua (unlike compiled languages like C, C++ etc.). Using ALLCAPS lets programmers know that these variables are constants and should not be changed (if they are named like other variables there is no easy way to tell).
You should be aware that because this is “just” a convention it means badly behaved code can (accidentally) update these (pseudo) CONST values.
Task [top]
Explain how (which part of) the code in the main module runs:
- When a channel starts up.
- When a channel processes a message.
Implementation [top]
Suppose you have the simple code below:
When you START this channel, here are the code execution steps:
Step | Module | Function | Line | Actions |
---|---|---|---|---|
1 | main.lua |
none | line 1 | set MAINCONST = 2 |
2 | main.lua | none | line 2 | jump into mymodule.lua |
3 | mymodule.lua | none | line 1 | set MODCONST = 1 → return to main.lua |
4 | main.lua | main() | line 4-5 | call mod.myfunction() → jump to mymodule.lua |
5 | mymodule.lua | mymodule.myfunction() | line 5-7 | return MODCONST → return to main.lua |
6 | main.lua | main() | line 6-7 | trace MAINCONST → end |
And these are the same steps in pictures:
- main.lua: set MAINCONST = 2
- main.lua: jump into mymodule.lua
- module.lua: set MODCONST = 1 → return to main.lua
- main.lua: call mod.myfunction() → jump to mymodule.lua
- module.lua: return MODCONST → return to main.lua
- main.lua: trace MAINCONST → end
When a MESSAGE triggers this channel, here are the code execution steps:
Step | Module | Function | Line | Actions |
---|---|---|---|---|
1 | main.lua | main() | line 4-5 | call mod.myfunction() → jump to mymodule.lua |
2 | mymodule.lua | mymodule.myfunction() | line 5-7 | return MODCONST → return to main.lua |
3 | main.lua | main() | line 6-7 | trace MAINCONST → end |
And these are the same steps in pictures:
- main.lua: call mod.myfunction() → jump to mymodule.lua
- module.lua: return MODCONST → return to main.lua
- main.lua: trace MAINCONST → end
How it works [top]
When you start a channel the Translator runs all the code in the main module — this only occurs once when the channel starts. When a running channel receives a message, from its queue, only the main() function is called. This means that initialization of “constant” variables (and functions) outside of the main()
function only occurs once when the channel starts.
When you START a channel: The Iguana Translator runs the complete main module once.
This is the result:
- The Translator will initialize any instance variables that are outside your
main()
function:- The values CONSTANT variables are initialized to will persist until the channel is stopped and restarted again.
Note: You could change set the values for CONSTANT variables in other code but this is not best practice (in fact it is a very bad idea).
- The values CONSTANT variables are initialized to will persist until the channel is stopped and restarted again.
- The Translator will make any function calls that are outside your
main()
function:- Functions calls outside
main()
can be useful for more complex initialization (one-off) tasks. - Our code example kept it simple and did not make any function calls outside
main()
.
- Functions calls outside
- Any “required” modules are also initialized:
- Variables declared within a module but outside a function (i.e., variables with module scope) are initialized once when the channel starts.
- Function calls within a module but outside a function (i.e., calls with module scope) are run once when the channel starts.
- Functions within modules are not run by a “require” command (unlike the
main()
function which is a special case on startup)
- The Translator also runs the
main()
function.
When a MESSAGE triggers a running channel: Iguana uses the main()
function as the entry point, and only runs the code within the main()
function.
This is the result:
- The Translator only runs the
main()
function.