Skip to content

Program Structure

Chuks is a multi-paradigm language that supports both Object-Oriented Programming (OOP) and Functional Programming (FP) styles seamlessly. You can mix and match these approaches within the same file.

A Chuks program may or may not require a main() function or an entry point class. It executes top-level statements sequentially, similar to scripting languages like Python or JavaScript.

Simply write code at the top level of your file.

hello.chuks
println("Hello, World!")

You can define a main function if you prefer structure, but that should be the entry point of your program.

app.chuks
function main() {
println("App Started");
}

Chuks supports forward references — you can call functions and reference classes that are declared later in the same file. The compiler performs a pre-pass to register all top-level declarations before execution.

Call a function before its definition:

function main() {
// greet() is defined below main — works fine
var msg = greet("World");
println(msg); // Hello, World!
println(add(10, 20)); // 30
}
function greet(name: string): string {
return "Hello, " + name + "!";
}
function add(a: int, b: int): int {
return a + b;
}

Use a class as a type annotation or instantiate it even if the class is defined later:

class App {
var handler: Handler // Handler is defined below
constructor(h: Handler) {
this.handler = h;
}
function run(): string {
return this.handler.handle("test");
}
}
class Handler {
var prefix: string;
constructor(prefix: string) {
this.prefix = prefix;
}
function handle(input: string): string {
return this.prefix + ": " + input;
}
}
function main() {
var h = new Handler("LOG");
var app = new App(h);
println(app.run()); // LOG: test
}

Chuks allows defining pure functions and passing them around as first-class citizens.

function double(n: int): int {
return n * 2;
}
const numbers = [1, 2, 3, 4, 5];
const doubled = [];
for (n in numbers) {
doubled.push(double(n));
}
println(doubled) // [2, 4, 6, 8, 10];

You can pass functions as arguments.

function apply(fn: (int) => int, val: int): int {
return fn(val);
}
const result = apply(double, 10); // 20

Chuks supports classes, interfaces, inheritance, and encapsulation.

interface Shape {
area(): float;
}
class Circle implements Shape {
protected radius: float;
constructor(r: float) {
this.radius = r;
}
public area(): float {
return 3.14159 * this.radius * this.radius;
}
}
class Cylinder extends Circle {
private height: float;
constructor(r: float, h: float) {
super(r);
this.height = h;
}
public volume(): float {
return this.area() * this.height;
}
}
const c = new Cylinder(5.0, 10.0);
println("Volume: " + c.volume());

The standard library itself is a mix of paradigms. For example, http functions are methods on a singleton object, while strings functions are stateless helpers.

import { http } from "std/http"
import { json } from "std/json"
// OOP for Data Structure
dataType User {
id: int
name: string
}
// Functional for Logic
async function fetchUser(id: int): Task<User> {
const url = `https://api.example.com/users/${id}`;
const resp = await http.get(url)
// Parse JSON into User struct
const data = json.parse(resp.body)
const user:User = {
id: data["id"],
name: data["name"]
};
return user;
}
// Script Entry Point
const u = await fetchUser(101);
println("Fetched User: " + u.name);