All of a module's public functions must be declared. A function declaration in Limbo is similar to a function prototype in C and C++. It provides type information about the arguments and the function's return values. This allows the compiler to perform type-checking that ensures type safety at run-time.
The general form of a Limbo function declaration is:
Function declarations are similar to data declarations. Use the keywordfunction_name: fn(arguments) :return_type;
fn to specify that the object is a "function" type.
Functions that are not declared by the module are private. They cannot be accessed by external modules. The compiler still performs type-checking against the function call(s) and the function definition.
The following example is a module that contains two functions, the public init function and a private function.
implement Command;
include "sys.m";
include "draw.m";
sys: Sys;
Command: module {
init: fn (nil: ref Draw->Context, argv: list of string);
};
init (nil: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
for (i := 1; i <= 10; i++) {
sys->print(" %2d %4d\n", i, sqr(i));
}
sqr (n: int): int {
return (n*n);
}
The declaration of the public init function, Line 9, is within the module declaration, Lines 8 through 10.
The function definition in Lines 19 through 21 is the private function. It is not declared in the module declaration.
The use of public and private functions is of greater consequence with respect to modules that are intended to be loaded by other modules. This is discussed more in Modules in Chapter 3.