Extending Types

Extending base types with custom methods is easy and allows for some powerful extensions. To start, you need to use the ext keyword and then the base type that you would like to extend. Use curly braces to signify that you want to add methods and then define your methods within them.


Let us take a look at an example, to extend an integer with a method to double its value.

// First the "ext" keyword, followed by the type you want to extend
ext int {
    // Then you can define one or more methods, by first specifying the return
    // type, then a function. "self" is defined to be the input value
    // see below for waht exactly that is.
    int double() {
        return self * 2

// Lets call it with the value 200, meaning that the return value will be 400

Last updated