Skip to content

Classes

Frederik Tobner edited this page Dec 17, 2022 · 4 revisions

A class is defiened using the class keyword followed by an optional parent class that is prefixed with a double dot, like in C++ or C#.

class foo : bar {
   
}

A constructor in cellox is called initializer and must be declared as a method inside the class.

The initializer method that initializes must be called init.

To refer to a method or a field of a cellox instance the this keyword followd by a dot is used.

class Point {
    init(x, y) {
        this.x = x;
        this.y = y;
    }   
}

An instance of this class is created like this:

var point = Point(1, 2);

The super keyword is used to refer to a method of the parent class.

class bar {
   init(x) {
        this.x = x;
   }
   print_foo() {
        printf(this.x);
   }
}
class foo : bar {
   init(x, y) {
        super(x);
        this.y = y;
   }
   print_bar() {
        super.print_foo()
        printf(this.y);
   }
}

The fields of the parent class can be accessed by using the this keyword like the field would belong to the child.

Class instances are by default serialized in a format that resembles json, when printed.

printf(foo(x, y));
Clone this wiki locally