Skip to main content
The 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

import "<path>"
The path is a double-quoted string literal. It is resolved relative to the working directory when the program is run.
import "utils"
import "lib/helpers"
import "config.ocat"

Module resolution

When you write import "path", the runtime tries three path variants in order:
1

Exact path

The runtime first checks whether the string you provided refers to an existing file exactly as written.
import "config.ocat"  // tries 'config.ocat' first
2

Append .ocat extension

If the exact path does not exist, the runtime appends .ocat and tries again.
import "config"  // tries 'config.ocat'
3

Append .oc extension

If neither of the first two paths exist, the runtime appends .oc and tries a third time.
import "config"  // tries 'config.oc'
You can omit the file extension when importing. Writing import "utils" will find utils.ocat or utils.oc automatically.

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.
// In utils.ocat:
string appVersion = "1.0.0"

func printVersion() {
    print(appVersion)
}
// In main.ocat:
import "utils"

print(appVersion)       // works — imported from utils
call printVersion()     // works — imported from utils

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
string defaultGreeting = "Hello!"

func greet() {
    print(defaultGreeting)
}

func greetFormal() {
    print("Good day.")
}
Running main.ocat produces:
Hello!
Good day.
Hello!

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 the import statement.
If you reference a variable or function from a module before the import line that loads it, the runtime will raise an UndeclaredVariableError or UndeclaredFunctionError.

Syntax overview

Keywords, identifiers, and overall program structure.

Functions

Defining and calling functions within and across files.

Build docs developers (and LLMs) love