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.
Starting a Program
Section titled “Starting a Program”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.
Script Mode (Simple)
Section titled “Script Mode (Simple)”Simply write code at the top level of your file.
println("Hello, World!")Main Function (Optional)
Section titled “Main Function (Optional)”You can define a main function if you prefer structure, but that should be the entry point of your program.
function main() { println("App Started");}Forward References
Section titled “Forward References”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.
Forward Function Calls
Section titled “Forward Function Calls”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;}Forward Class References
Section titled “Forward Class References”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}Functional Style
Section titled “Functional Style”Chuks allows defining pure functions and passing them around as first-class citizens.
Map/Filter Logic
Section titled “Map/Filter Logic”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];Higher-Order Functions
Section titled “Higher-Order Functions”You can pass functions as arguments.
function apply(fn: (int) => int, val: int): int { return fn(val);}
const result = apply(double, 10); // 20Object-Oriented Style
Section titled “Object-Oriented Style”Chuks supports classes, interfaces, inheritance, and encapsulation.
Class Definition
Section titled “Class Definition”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; }}Inheritance
Section titled “Inheritance”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());Mixing Paradigms
Section titled “Mixing Paradigms”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.
Async HTTP + DataType Modeling
Section titled “Async HTTP + DataType Modeling”import { http } from "std/http"import { json } from "std/json"
// OOP for Data StructuredataType User { id: int name: string}
// Functional for Logicasync 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 Pointconst u = await fetchUser(101);println("Fetched User: " + u.name);