Methods in AngelScript

class rect {
    int width;
    int height;
}

int area(rect@ r) {
    return r.width * r.height;
}

int perim(rect r) {
    return 2*r.width + 2*r.height;
}

void main() {
    rect r = {width: 10, height: 5};

    // Here we call the 2 functions defined for our struct.
    print("area: " + area(r));
    print("perim: " + perim(r));

    // AngelScript does not have automatic reference handling like Go,
    // so we need to explicitly pass a reference when calling 'area'.
    print("area: " + area(@r));
    print("perim: " + perim(r));
}

AngelScript supports methods defined on struct types, but the syntax and behavior are slightly different from Go. Here’s an explanation of the changes:

  1. In AngelScript, we define a class instead of a struct. The syntax is similar, but classes are more common in AngelScript.

  2. Instead of defining methods with a receiver, we define regular functions that take the struct as a parameter. The area function takes a reference parameter (rect@), while perim takes a value parameter.

  3. The @ symbol in AngelScript is used to denote references. We use it when defining the area function parameter and when calling the function.

  4. AngelScript doesn’t have an exact equivalent to Go’s automatic handling of values and pointers for method calls. We need to explicitly pass a reference when calling area.

  5. The print function is used instead of fmt.Println for output.

  6. String concatenation in AngelScript uses the + operator.

To run this program in AngelScript, you would typically embed it in a host application or use an AngelScript interpreter. The exact process may vary depending on your setup.

This example demonstrates how to define functions that operate on custom types in AngelScript, which is conceptually similar to methods in Go. The main difference is that AngelScript uses a more traditional function-based approach rather than Go’s receiver syntax.