Module System
Flowa provides a powerful module system for organizing code across multiple files and creating reusable components.
Exporting from Modules
Use the export keyword to make variables and functions available to other files:
module_a.flowa:
export let message = "Hello from Module A";
export func greet(name) {
return "Hello, " + name;
}
export let version = "1.0.0";Note: Exported symbols must be declared with export before the let or func keyword.
Importing from Modules
Use the import statement with destructuring syntax to import specific symbols:
import { message, greet } from "path/to/module_a.flowa";
print(message); // "Hello from Module A"
print(greet("World")); // "Hello, World"Module Path Resolution
Flowa resolves module paths relative to the current file or script execution directory:
// Relative to current file
import { utils } from "./utils.flowa";
import { config } from "../config/settings.flowa";
// Relative to execution directory
import { helpers } from "src/helpers.flowa";Module Execution
- Modules are executed once when first imported
- Subsequent imports reuse the already-loaded module
- Module code runs in isolated scope
- Exports are cached for performance
Example
counter.flowa:
let count = 0;
export func increment() {
count = count + 1;
return count;
}
export func getCount() {
return count;
}main.flowa:
import { increment, getCount } from "counter.flowa";
print(increment()); // 1
print(increment()); // 2
print(getCount()); // 2Best Practices
1. Use Clear Names
// Good
export func calculateTotal(items) { ... }
// Avoid
export func calc(x) { ... }2. Group Related Exports
// math_utils.flowa
export func add(a, b) { return a + b; }
export func subtract(a, b) { return a - b; }
export func multiply(a, b) { return a * b; }3. Avoid Circular Dependencies
Avoid having a.flowa import b.flowa which imports a.flowa.
4. Module Organization
project/
├── main.flowa
├── utils/
│ ├── string.flowa
│ └── array.flowa
└── services/
├── auth.flowa
└── database.flowa