Modules
require(name) loads another .flx file and gives you back its top-level declarations as a single value.
// player.flx
struct Player {
health = 100;
func takeDamage = define(amount) { health -= amount; }
}
let playerVersion = "1.0";
// main.flx
let player = require("player");
let p = player.Player();
output(p.health); // 100
p.takeDamage(25);
output(p.health); // 75
output(player.playerVersion); // "1.0"
Everything declared at the top level of the required file - let/const variables, funcs, and structs - becomes a property on the returned module, accessed with dot syntax exactly like the module were a map. There's no explicit export keyword or list; it's everything or nothing.
Path resolution
- Don't include the
.flxextension in the string you pass -.flxis appended automatically. - A name is resolved relative to the file doing the requiring, not relative to the entry script.
require("utils/helper")looks forutils/helper.flxnext to whichever file contains thatrequirecall. - Subdirectories work - the name can contain
/. - The resolved file must live inside the project directory, which is the directory containing the entry script you passed to
run(or the current working directory, for the REPL). Trying torequireyour way outside of it - with enough../segments, for example - raisesModuleNotFoundException, the same error you'd get for a genuinely missing file.
// resolves to utils/helper.flx, relative to the requiring file:
let helper = require("utils/helper");
Caching & circular requires
Each resolved file path is only ever parsed and executed once per run - requiring the same module twice (even from two different files) returns the same cached module value. Requiring a module that is (directly or indirectly) already in the middle of being loaded raises CircularModuleDependencyException rather than hanging or silently returning a half-initialized module.
Which file to run
Point python -m src.Main run (see the CLI reference) at the entry file - the one that requires the others - not at a file meant to be required. Running a "library" file directly still works (it just executes top to bottom like any script), but its own require calls will then resolve relative to it instead of to whatever originally required it.