AutolangDocs

Classes & Inheritance

Autolang supports object-oriented modeling using classes, single inheritance, primary constructors, and operator overloading.

Primary Constructor

A primary constructor is declared in the class header. Parameters defined with val or var automatically become properties of the class with default or custom values.

class CustomerAccount(val id: String, var balance: Float, val tier: String = "STANDARD") { fun credit(amount: Float) { this.balance += amount } fun describe(): String = "Account " + this.id + " [" + this.tier + "] Balance: " + this.balance } val account = CustomerAccount("CUST-104", 500.0) account.credit(150.0) println(account.describe())

Secondary Constructors & Inheritance

Use extends to inherit from a base class. Secondary constructors are declared with the constructor keyword, and super(...) must be the first invocation when subclassing.

class BaseService(val serviceName: String, val timeoutMs: Int) { fun describe(): String = serviceName + " (timeout: " + timeoutMs + "ms)" } class PaymentService extends BaseService { var gatewayEndpoint: String = "" constructor(serviceName: String = "PaymentGateway", endpoint: String = "https://pay.internal") { super(serviceName, 5000) this.gatewayEndpoint = endpoint } } val payment = PaymentService() println(payment.describe()) println("Endpoint: " + payment.gatewayEndpoint)

Method Overriding

Subclasses override base class methods using either the override fun keyword or the @override annotation. To disallow overriding in further subclasses, mark methods with @no_override.

class NotificationChannel { fun send(recipient: String, message: String): Bool { return false } } class EmailChannel extends NotificationChannel { constructor() { super() } override fun send(recipient: String, message: String): Bool { println("Dispatching email to " + recipient + ": " + message) return true } } val channel: NotificationChannel = EmailChannel() channel.send("ops@enterprise.com", "Deployment completed")

Static Members & Local Static Variables

Declare class-wide static state using static val, static var, and static fun. Additionally, methods can declare local static variables that retain state across calls.

class LedgerRegistry { static var totalTransactions: Int = 0 static fun recordTransaction(amount: Int) { totalTransactions += 1 } fun generateTrackingNumber(): Int { static var localTracker = 5000 localTracker += 1 return localTracker } } LedgerRegistry.recordTransaction(100) LedgerRegistry.recordTransaction(250) println("Total recorded: " + LedgerRegistry.totalTransactions) // 2 val ledger = LedgerRegistry() println("Tracking: " + ledger.generateTrackingNumber()) // 5001 println("Tracking: " + ledger.generateTrackingNumber()) // 5002

Operator Overloading

Autolang classes can overload standard operators using the operator fun keyword prefix (or @operator annotation).

class CurrencyAmount(val amount: Int) { // Overload binary '+' operator operator fun plus(other: CurrencyAmount): CurrencyAmount { return CurrencyAmount(this.amount + other.amount) } // Overload binary '-' operator operator fun minus(other: CurrencyAmount): CurrencyAmount { return CurrencyAmount(this.amount - other.amount) } // Overload comparisons (<, <=, >, >=) operator fun compareTo(other: CurrencyAmount): Int { return this.amount - other.amount } } val amount1 = CurrencyAmount(1500) val amount2 = CurrencyAmount(500) val combined = amount1 + amount2 println("Combined amount: " + combined.amount) // 2000 println("Amount 1 is greater? " + (amount1 > amount2)) // true

Index & Membership Operators

Implement operator fun get, operator fun set, and operator fun contains to enable index access [] and membership checking in on custom collections.

class RateTable(val rates: Array<Int>) { operator fun get(index: Int): Int = rates[index] operator fun set(index: Int, value: Int) { rates[index] = value } operator fun contains(target: Int): Bool = target in rates } val table = RateTable([10, 25, 50]) table[0] = 15 println("Rate at 0: " + table[0]) // 15 println("Contains 25: " + (25 in table)) // true