Extensions

With a extension you can add functions/methods to an existing type. These are useful when you want a type to have extra helper methods, but you do not have access to the definition of this type. Any of the existing Lunaris types can be extended.

Primitive Types

extension string {
    function Append(s) {
        return this + s;
    }
}

print("Hello".append(" World")) // Hello World
extension number {
    function IsEven() {
        return this % 2 == 0;
    }
}

print(2.IsEven()) // true
print(3.IsEven()) // false
extension boolean {
    function Invert() {
        return !this;
    }
}

print(true.invert()) // false
print(false.invert()) // true

Class

Classes can be extended. This goes for both classes defined in other modules as classes that Erios has.

extension Color {
    function Transparent() {
        this.A = 200;
        return this;
    }
}

local color = new Color('ff6400') // Color has Alpha of 255
local transparent = color.Transparent() // Color has Alpha of 200

Categories:

Updated: