Skip to main content

Classes, Objects & Interfaces Reference

Kyna supports object-oriented programming with single inheritance, structural interfaces, constructors via init, encapsulation modifiers (public, private), and instance binding through self.


Defining a Class​

Classes are declared using the class keyword. Instance variables and methods are declared with access visibility modifiers.

class DatabaseConnection {
private host: str;
private port: int;
private isConnected: bool;

public init(host: str, port: int) {
self.host = host;
self.port = port;
self.isConnected = false;
}

public fn connect(): bool {
self.isConnected = true;
print("Connected to database at", self.host + ":" + self.port);
return true;
}

public fn getStatus(): str {
return if (self.isConnected) "ONLINE" else "OFFLINE";
}
}

# Instantiation using 'new'
var db = new DatabaseConnection("db.internal.kyna", 5432);
db.connect();
print("Status:", db.getStatus()); # ONLINE

Inheritance & Method Overriding​

Use extends to inherit from a base class. To override a virtual method in a child class, use the override modifier.

class HttpRequest {
public url: str;
public method: str;

public init(url: str, method: str) {
self.url = url;
self.method = method;
}

public fn summary(): str {
return self.method + " " + self.url;
}
}

class JsonHttpRequest extends HttpRequest {
public body: str;

public init(url: str, method: str, body: str) {
super.init(url, method);
self.body = body;
}

public override fn summary(): str {
return "[JSON] " + super.summary() + " | Body Bytes: " + len(self.body);
}
}

var req = new JsonHttpRequest("https://api.kyna.dev/users", "POST", "{\"name\":\"Ada\"}");
print(req.summary());
# Outputs: [JSON] POST https://api.kyna.dev/users | Body Bytes: 14

Structural Interfaces: intf​

Interfaces in Kyna define contracts. Any class that implements the required method signatures satisfies the interface contract either explicitly via implements or structurally.

intf Logger {
log(message: str): void;
}

class ConsoleLogger implements Logger {
public fn log(message: str): void {
print("[LOG]", message);
}
}

class FileLogger implements Logger {
private filename: str;

public init(filename: str) {
self.filename = filename;
}

public fn log(message: str): void {
# File writing logic
print("[FILE " + self.filename + "]", message);
}
}

fn executeWithLogger(logger: Logger, text: str) {
logger.log(text);
}

executeWithLogger(new ConsoleLogger(), "System booted.");
executeWithLogger(new FileLogger("server.log"), "User logged in.");

Ambient Type Definitions (.kyna.d)​

Kyna supports ambient declaration files (.kyna.d, .d.ky) for declaring third-party interfaces and global contract types without generating runtime bytecode.