Top-Level Code
In Strata, the top level of a file is primarily reserved for declarations. This encourages better code organization and ensures that execution logic is contained within functions and methods.
What Can Be at the Top Level
The following declarations are allowed at the top level:
Declarations
- Namespace declarations:
namespace NamespaceName; - Imports: Import declarations
import ...; - Function declarations:
fn functionName() - Class declarations:
class ClassName() - Interface declarations:
interface InterfaceName - Trait declarations:
trait TraitName - Type declarations:
type TypeAlias = ...;(see Types) - Variable declarations:
let variableName: Type = value;(for file-level or global-like variables)
Simple Statements
- Echo/Print:
echoorprint()calls for simple output. - Require:
require path;for composition.
Note: echo and print only accept printable types: String, Int, Float, Bool, Mixed, or union types where all members are printable.
What Cannot Be at the Top Level
To maintain structure and safety, all control flow and complex execution logic must reside inside functions or methods. The following cannot appear at the top level:
- Control Flow:
if,while,for-each,match - Error Handling:
try/catch,throw - Control Jump:
return,break,continue,goto - State Mutation: Assignment to existing variables (outside of
letdeclarations)
Entry Point: The main() Function
For executable logic, the idiomatic pattern is to use a main() function. In the entry file, this function runs automatically.
strata
import App.Models.User;
fn main(): Void {
try {
let user = loadUser(id: 1);
print("Hello, ${user.unwrap().name}");
} catch (exception) {
print("Error: " . exception.getMessage());
}
}Best Practices
- Declarations Only: Keep the top level for defining your program's structure.
- Use
main(): All entry-point logic should live inmain(). - Encapsulation: Organize all complex logic into functions or class methods.
Summary Table
| Allowed at Top Level | Forbidden at Top Level |
|---|---|
namespace | if / else / match |
import | while / for-each |
type | try / catch / throw |
fn / class | return |
interface / trait | break / continue |
let (declaration) | Assignment (mutation) |
print() / echo() | |
require |