import statement loads another OCat file and makes its variables and functions available in the current file. This lets you split a program into modules and reuse shared logic.
Syntax
Module resolution
When you writeimport "path", the runtime tries three path variants in order:
Exact path
The runtime first checks whether the string you provided refers to an existing file exactly as written.
What gets imported
When a module is loaded, its entire file is executed. All variables and functions that end up in the module’s top-level context are then merged into the importing file’s context:- Every variable declared at the top level of the imported file becomes available by name.
- Every function defined at the top level of the imported file becomes available by name.
Caching
Each module is executed at most once per program run. If two files both import the same module, the runtime returns the cached context from the first load rather than executing the file a second time.Circular imports (where file A imports file B which imports file A) are not detected explicitly. Avoid creating circular import chains.
Multi-file example
- greetings.ocat
- main.ocat
main.ocat produces:
Import placement
Imports can appear anywhere in a file where a statement is valid, but it is conventional to place them at the top of the file before any other statements. Variables and functions from an import are available on all lines after theimport statement.
Syntax overview
Keywords, identifiers, and overall program structure.
Functions
Defining and calling functions within and across files.