Skip to content

Classes

Isaac Shelton edited this page Sep 8, 2023 · 10 revisions

Classes

Classes are structures which can have virtual methods defined on them.

import basics

class Shape () {
    constructor {}

    virtual func getArea() float = 0.0
}

class Rectangle extends Shape (w, h float) {
    constructor(w, h float){
        this.w = w
        this.h = h
    }

    override func getArea() float = this.w * this.h
}

func main {
    shape *Shape = new Rectangle(5.0f, 10.0f)
    defer delete shape
    print(shape.getArea())
}

All classes require a constructor and instances of classes must be constructed in order to use dynamic dispatch / virtual methods!

Classes also come with a __vtable__ field, which is implicitly initialized in class constructors. The layout of each vtable is not consistent and is only intended for use by the compiler. The only real purpose the programmer can use this value for is to determine if two values use the same vtable.

Warning about calling parent constructors

This issue was fixed in v2.8 with the introduction of super() which you can use to call parent constructors.

Unfortunately, in Adept v2.7 (when constructors were introduced), calling parent constructors was not properly implemented!

Even if you know what you are doing, a work-around is not possible without -Onothing, as the LLVM 14 optimizer will accidentally perform incorrect optimizations!

So if you are still using v2.7, please use regular methods if you wish to share code between constructors of different classes.

Clone this wiki locally