Object

Object is the implicit root class. Every user-defined class (created with Class:Create) automatically extends Object and inherits its three methods. Built-in modules (Konsol, Math, File, etc.) are handlers and do not extend Object.


Object methods

All instances of user-defined classes have these methods available:

Method Returns Description
.getType() String Class name of this instance
.toString() String "[ClassName]"
.isInstanceOf(String t) Boolean true if this instance is a t or subclass of t
Class:Create(Animal) {
    Var:String name;
}

Var:Animal a;
a.name = "Rex";

Var:String t;
t = a.getType();
Konsol:Print(t);                        // Animal

Var:String s;
s = a.toString();
Konsol:Print(s);                        // [Animal]

Var:Boolean check;
check = a.isInstanceOf("Animal");
Konsol:Print(check);                    // true

check = a.isInstanceOf("Object");
Konsol:Print(check);                    // true

Exception

Exception extends Object and is the structured error carrier used by try / catch. Plugin typed exceptions (e.g. MySQLException) also extend Exception.

Fields

Field Type Description
message String Human-readable error description
type String Exception type name
code Number Numeric code (0 if unused)

Methods

Method Description
.init(String type, String message) Set type and message fields at once
.toString() Returns "[type] message"
.getType() Returns "Exception" (inherited from Object)
.isInstanceOf(String t) true for "Exception" or "Object"

Usage in try/catch

try {
    throw "something went wrong";
} catch (Exception e) {
    Konsol:Print(e.message);    // something went wrong
    Konsol:Print(e.type);       // Exception
    Konsol:Print(e.code);       // 0
}

When a plugin throws a typed exception (e.g. MySQLException), the caught instance exposes the same message, type, and code fields:

try {
    MySQL:Connect("localhost", "root", "wrongpass", "mydb", db);
} catch (MySQLException e) {
    Konsol:Print("MySQL error ${e.code}: ${e.message}");
} catch (Exception e) {
    Konsol:Print("Unexpected: ${e.message}");
}

catch (Exception e) is the generic catch-all that matches any typed plugin exception. More specific types must be named exactly.


Examples

// 18_object.ks — Object root class: getType, toString, isInstanceOf

Konsol:Print("=== Object Root Class ===");

// Every class implicitly extends Object now.
// No 'extends' keyword needed — the engine wires it automatically.

Class:Create(Animal) {
    Var:String name;
    function setName(String n) { name = n; }
}

Class:Create(Dog extends Animal) {
    Var:String breed;
    function setBreed(String b) { breed = b; }
}

Class:Create(Cat extends Animal) {
    Var:String color;
}

Class:Create(Vehicle) {
    Var:Number wheels;
}

// ── getType ───────────────────────────────────────────────────────────────────
Konsol:Print("--- getType ---");

Class:Dog d;
d.setName("Rex");
d.setBreed("Labrador");
d.getType();
Konsol:Print("dog getType = ${_ret}");

Class:Cat c;
c.name = "Whiskers";
c.getType();
Konsol:Print("cat getType = ${_ret}");

Class:Vehicle v;
v.wheels = 4;
v.getType();
Konsol:Print("vehicle getType = ${_ret}");

// ── toString ──────────────────────────────────────────────────────────────────
Konsol:Print("--- toString (default) ---");

d.toString();
Konsol:Print("dog toString = ${_ret}");

c.toString();
Konsol:Print("cat toString = ${_ret}");

v.toString();
Konsol:Print("vehicle toString = ${_ret}");

// ── toString override ─────────────────────────────────────────────────────────
Konsol:Print("--- toString override ---");

Class:Create(Point) {
    Var:Number x;
    Var:Number y;

    function toString() {
        // Override Object::toString with something meaningful
        _ret = "Point(" + x + ", " + y + ")";
    }
}

Class:Point p;
p.x = 3;
p.y = 7;
p.toString();
Konsol:Print("point toString = ${_ret}");

p.getType();
Konsol:Print("point getType  = ${_ret}");

// ── isInstanceOf ──────────────────────────────────────────────────────────────
Konsol:Print("--- isInstanceOf ---");

d.isInstanceOf("Dog");
Konsol:Print("dog isInstanceOf Dog    = ${_ret}");

d.isInstanceOf("Animal");
Konsol:Print("dog isInstanceOf Animal = ${_ret}");

d.isInstanceOf("Object");
Konsol:Print("dog isInstanceOf Object = ${_ret}");

d.isInstanceOf("Cat");
Konsol:Print("dog isInstanceOf Cat    = ${_ret}");

d.isInstanceOf("Vehicle");
Konsol:Print("dog isInstanceOf Vehicle= ${_ret}");

c.isInstanceOf("Cat");
Konsol:Print("cat isInstanceOf Cat    = ${_ret}");

c.isInstanceOf("Animal");
Konsol:Print("cat isInstanceOf Animal = ${_ret}");

c.isInstanceOf("Dog");
Konsol:Print("cat isInstanceOf Dog    = ${_ret}");

v.isInstanceOf("Vehicle");
Konsol:Print("veh isInstanceOf Vehicle= ${_ret}");

v.isInstanceOf("Animal");
Konsol:Print("veh isInstanceOf Animal = ${_ret}");

// ── Polymorphic-style dispatch using getType ──────────────────────────────────
Konsol:Print("--- type-based dispatch ---");

function describeAnimal(String typeName, String name) {
    if (typeName == "Dog") {
        Konsol:Print("${name} is a dog -- fetch!");
    } else if (typeName == "Cat") {
        Konsol:Print("${name} is a cat -- meow!");
    } else {
        Konsol:Print("${name} is an unknown animal");
    }
}

Class:Dog d2;
d2.name = "Buddy";
d2.getType();
describeAnimal(_ret, d2.name);

Class:Cat c2;
c2.name = "Luna";
c2.getType();
describeAnimal(_ret, c2.name);