Top-level items and modules
This page covers the syntax that can appear at top level in a source file.
Imports
Canonical form:
import { connect, listen as serve } from net.http
Supported details:
- brace import list
- aliases with
as - dotted module path after
from - multiline import item list
Example:
import {
fs as f
net as n
} from std.io
Functions
Top-level functions:
fn main() {
return 0
}
Public top-level functions:
pub fn build() {
return 0
}
Classes
Classes may include:
- fields
- methods
- layers
- class-level
@typeblocks - inheritance with
extends - interface implementation with
implements - generic parameters
Example:
class User extends Entity implements Named {
let label
}
Interfaces
Interfaces contain method declarations and interface-level @type blocks:
interface Drawable {
fn draw()
@type {
draw: () -> Int
}
}
Enums
Enum syntax:
enum Direction { North, South, East, West }
Errors
Error syntax:
error NotFound(msg)
error NotFound(msg: String)
The parser accepts parameter syntax similar to function parameter declarations.
Constants
Constant syntax:
const ANSWER = 42
Type blocks
Top-level file contracts:
@type {
add: (Int, Int) -> Int
}
Extern blocks
Extern block syntax:
@extern "C" {
fn puts(msg)
}
Accepted details:
- ABI string after
@extern - function declarations inside the block
- parameter hints and return types are accepted by the parser here as well
Panic and OOM handlers
Top-level special handlers:
@panic_handler
fn on_panic(msg) { }
@oom_handler
fn on_oom() { }
These are parser-level items, not ordinary attributes attached to arbitrary declarations.