Skip to content

Functions

Functions

Functions are declared with func. Parameters always have explicit types. Return types are optional because the compiler can infer them from return statements.

Basic syntax

func add(left: i32, right: i32) -> i32:
return left + right

Implicit return type inference

If you omit the return annotation, the type checker infers it from the explicit return statements.

func double(value: i32):
return value * 2

The inferred return type of double is i32.

If a function has no explicit return value, the inferred type is void:

func greet(name: str):
println(name)

The compiler also checks consistency across multiple return paths:

func abs_value(value: i32):
if (value < 0):
return -value
return value

Builtin constraints for generics

The current compiler supports generic functions with these built-in constraints:

ConstraintMeaning
Orderedsupports ordering comparisons
Eqsupports equality comparisons
Numericsupports arithmetic

Generic functions

func abs<T: Numeric>(value: T) -> T:
if (value < 0):
return -value
return value

Call-site inference chooses the concrete type from the arguments:

func main() -> i32:
let left: i32 = abs(-5)
let right: f64 = abs(-3.5)
if (left == 5 and right == 3.5):
return 0
return 1

Multiple type parameters

The parser accepts multiple generic parameters and generic type syntax:

func choose_left<T: Eq>(left: T, right: T) -> T:
if (left == right):
return left
return left

Recursion

Recursive functions are supported:

func fib(n: i32) -> i32:
if (n <= 1):
return n
return fib(n - 1) + fib(n - 2)

Current boundary

The current compiler fully supports generic functions end to end. Generic struct and impl surface syntax is parsed and represented in the AST, but it is not yet available as a complete user-facing feature across the full pipeline.