GW supports organizing code into multiple files using the import keyword. This works similarly to the C/C++ preprocessor: the content of the imported file is directly substituted in place of the import statement.
import "filename.gw"
The path is relative to the file containing the import statement.
- The
FileReaderscans the source code for lines starting withimport. - When found, it resolves the path of the imported file.
- It reads the content of that file and replaces the
importline with it. - This process is recursive: imports inside imported files are also processed.
- Circular Dependency Protection: The system keeps track of included files to prevent infinite loops (e.g., A imports B, B imports A). A file is only included once per compilation unit.
func square(x: Number) {
return x * x;
}
import "math_utils.gw"
var result = square(5);
println(result); // Output: 25
Imports work with subdirectories as well:
project/
├── main.gw
└── lib/
└── utils.gw
In main.gw:
import "lib/utils.gw"
In lib/utils.gw, you can import other files relative to lib/:
import "other_util.gw" // Looks for project/lib/other_util.gw