Using Modules
Using modules is another way to convert handlers in function values but the main difference is that these handlers are some kind of “ready to use” as function values, they are intentionally written to use them as function values and organized in modules.
Each module is a parent script named with the prefix “fnModule-”, the rest is the module name.
Inside each module we can write functions as normal handlers, each handler will be private unless we write immediatly before the handler another “dummy handler” that “publish” the handler.
This dummy handler will have the form: on public_{public function name}_{number of parameters}
For example Add could be defined in a Math Module like this:
on public_add_2
on plus me, x,y
return x+y
end
Note that the public name and the private name don’t need to be same.
Now we can use the LingoF open method to load the entire module in a variable:
$ = LingoF().open(#Math)
put $.add[2][3]
-- 5
Note: I like to use the $ variable to store LingoF instances, just because it’s name contains no letters. If you yet didn’t noticed, Lingo allows to define identifiers with unused symbols like $, %, @ but they can’t contain more characters.
The exception is the _ which can be followed by any valid identifier.
Inside a function definition you can call a function from the same module this way:
me.{public function name}
This is an example of the factorial function which calls itself:
on public_factorial_1
on fact me, n
if n=0 then return 1
else return n*me.factorial[n-1]
end
If the function you want to call is defined in another module you can declare a property at the beginning of the parent script whit the name of the module you want to use:
property LingoF
... -- inside a handler
me.LingoF.Copy(lst)
...
But take care, don’t cross reference modules. The modules should be organized in hierarchical way to avoid cross references.
Included modules
At this stage the framework include some basic modules:
- LingoF: Most common function values are defined here. The framework itself uses functions values stored here. This module is automatically opened using the open method.
- Lingo: Here we have a collection of standard Lingo functions ready to use as function values. Note that Lingo has functions that accept a multiple number of parameters, like List, PropList, Call, etc. This functions will accept instead a linked list (not confuse with lingo’s linear lists and property lists).
- Shortcuts: Here is where shortcuts/syntax helpers are implemented.
- Lists: List related functions such as map, filter, etc
- Strings: String related functions.