2: Imports [✓] - royal-lang/rl GitHub Wiki
Since Royal is a module based language then instead of including header files you simply import the modules you want to use symbols from.
If no specific symbols are specified then all public symbols will be usable.
import MODULE;
import MODULE : MEMBERS;
Example:
import std.stdio;
import std.stdio : writeln;
import std.stdio : writeln readln; // Members are separated by white-spaces.
Imports doesn't necessarily have to be specified within the global scope of a module. They can be specified locally to a scope within a function etc. which will localize symbols imported to the specific scope.
Ex.
fn myFunction()
{
import std.stdio;
// std.stdio symbols are available here.
}
// std.stdio symbols are not available here.
Automatic Import
If a symbol is not found then the compiler will automatically attempt to find the associated module for the symbol and import that module's member.
This will allow avoiding import statements in some cases where you ex. only need to use a member once in a scope etc.
The compiler will only resolve symbols that aren't ambiguous. Ex. if no initial module is given to a symbol and a symbol with the same name exists in multiple modules then the symbol will not be resolved and the compiler will emit an error because the symbol wasn't found.
For faster compilation automatic imports can be disabled when compiling which will disable the compiler from doing automatic imports. This is of course not recommended in most cases.
Example:
The following example will compile and print out Hello World! despite the import to "std.stdio" missing.
fn main()
{
writeln("Hello World!");
}
The following will also work and will compile slightly faster because we initially gives the module that the symbol is a part of:
fn main()
{
std.stdio.writeln("Hello World!");
}
Both of the examples above will be translated by the compiler to:
fn main()
{
import std.stdio : writeln;
writeln("Hello World!");
}