Skip to main content
Version: Next

Extension Methods

Usage

Methods can be added to any type at any time. When searching for a method to call, Amethyst will see if any functions exist that satisfy the following conditions:

  • The first argument is an optional reference to the target type or to a base class of the target type.
  • The function is in the same namespace and path as the type, including the name of the type.

For example, this adds an extension method to all objects:

void foo() {
    int x = 8;
    string y = "yay";

    x.say();
    y.say();
}

namespace minecraft:nbt;

void say(macro nbt& this) {
    // print dereferences pointers automatically
    print(this);
}
note

While it is not necessary to make the this parameter a reference, it is highly recommended to do so. Entities are the only built-in objects that don't follow this rule. Additionally, setting this to be a macro parameter will allow Amethyst to generate simpler code.