Objects

Kairo has strcuts classes, enums, and interfaces (a c++ concept). Unions are not supported yet.

// Define a struct
struct Point {
    var x: i32;
    var y: i32;
}

// Define a class
class Circle {
    var center: Point;
    var radius: f32;

    fn Circle(self, center: Point, radius: f32) {
        self.center = center;
        self.radius = radius;
    }

    fn area(self) -> f32 {
        return 3.14 * self.radius * self.radius;
    }
}

// for generics you can do this
class <T> Boxed; // generic class


/// you can also define a constaint on generics (as long as the interface is defined or its a c++ concept)
class <T impl SomeInterface> ConstrainedBoxed;
fn main() {
    var p: Point = Point { x: 10, y: 20 };
    var c: Circle = Circle(p, 5.0);

    std::print(f"Circle area: {c.area()}");
}

Note a struct CAN NOT have functions, methods, or constructors; only data members and operators. Classes can have both data members and methods.

Enums are simple to define and use:

enum Color {
    Red,
    Green,
    Blue,
}

enum Something derives u8 { // underlying type u8
    First  = 1,
    Second = 2,
    Third  = 3,
}

fn main() {
    var c: Color = Color::Red;
    switch (c) {
        case Color::Red {
            std::print("Color is Red");
        } case Color::Green {
            std::print("Color is Green");
        } case Color::Blue {
            std::print("Color is Blue");
        }
    }
}

Interfaces are like c++ concepts, they define a set of methods that a class must implement to satisfy the interface.

interface Drawable {
    fn draw(self);
}

class Circle impl Drawable {
    fn draw(self) {
        std::print("Drawing a circle");
    }
}

fn <T impl Drawable> render(obj: T) {
    obj.draw();
}

fn main() {
    var c: Circle = Circle();
    render(c); // prints "Drawing a circle"
}

Interfaces right now dont support data members, only functions and operators.