Modules
Organizing code with the RAM module system
Module Declarations
To declare a module, use the mod keyword followed by the module name:
This tells the RAM compiler to look for a file named mymodule.ram in the same directory as the current file.
Importing from Modules
To use items from a module, you need to import them using the use keyword:
Nested Modules
Modules can be nested to create a hierarchical structure:
In this case, the compiler will look for files named math/basic.ram and math/advanced.ram.
Importing Nested Items
You can import items from nested modules using the path syntax:
You can also use curly braces to import multiple items from the same module:
Example
Here's a complete example of how to use modules in RAM:
main.ram:
math/basic.ram:
math/advanced.ram:
Benefits of Using Modules
Using modules in your RAM programs provides several benefits:
- Code Organization: Modules help you organize your code into logical units.
- Code Reuse: You can reuse code across different programs by importing modules.
- Namespace Management: Modules create separate namespaces, reducing the risk of name conflicts.
- Maintainability: Smaller, focused modules are easier to understand and maintain.
The module system in RAM is designed to be familiar to developers who have experience with modern programming languages like Rust, making it easier to organize and structure larger programs.