Slices in AngelScript

import string;
import array;

void main() {
    // Unlike arrays, slices are typed only by the
    // elements they contain (not the number of elements).
    // An uninitialized slice equals to null and has
    // length 0.
    array<string> s;
    print("uninit: " + s + " " + (s is null) + " " + (s.length() == 0));

    // To create an empty slice with non-zero length, use
    // the array constructor. Here we make a slice of
    // strings of length 3 (initially empty strings).
    s = array<string>(3);
    print("emp: " + s + " len: " + s.length() + " cap: " + s.capacity());

    // We can set and get just like with arrays.
    s[0] = "a";
    s[1] = "b";
    s[2] = "c";
    print("set: " + s);
    print("get: " + s[2]);

    // length() returns the length of the slice as expected.
    print("len: " + s.length());

    // In addition to these basic operations, slices
    // support several more that make them richer than
    // arrays. One is the insertLast method, which
    // adds one or more new values to the end of the slice.
    s.insertLast("d");
    s.insertLast("e");
    s.insertLast("f");
    print("apd: " + s);

    // Slices can also be copied. Here we create a new
    // slice c and copy the contents of s into it.
    array<string> c = s;
    print("cpy: " + c);

    // Slices support a "slice" operator with the syntax
    // slice.slice(start, end). For example, this gets a slice
    // of the elements s[2], s[3], and s[4].
    array<string> l = s.slice(2, 5);
    print("sl1: " + l);

    // This slices up to (but excluding) s[5].
    l = s.slice(0, 5);
    print("sl2: " + l);

    // And this slices up from (and including) s[2].
    l = s.slice(2);
    print("sl3: " + l);

    // We can declare and initialize a variable for slice
    // in a single line as well.
    array<string> t = {"g", "h", "i"};
    print("dcl: " + t);

    // AngelScript doesn't have a built-in slices package,
    // so we'll implement a simple equality check ourselves.
    array<string> t2 = {"g", "h", "i"};
    if (arraysEqual(t, t2)) {
        print("t == t2");
    }

    // Slices can be composed into multi-dimensional data
    // structures. The length of the inner slices can
    // vary, unlike with multi-dimensional arrays.
    array<array<int>> twoD(3);
    for (uint i = 0; i < 3; i++) {
        uint innerLen = i + 1;
        twoD[i].resize(innerLen);
        for (uint j = 0; j < innerLen; j++) {
            twoD[i][j] = int(i) + int(j);
        }
    }
    print("2d: " + twoD);
}

bool arraysEqual(array<string>@ a, array<string>@ b) {
    if (a.length() != b.length()) return false;
    for (uint i = 0; i < a.length(); i++) {
        if (a[i] != b[i]) return false;
    }
    return true;
}

Note that while slices are different types than arrays, they are rendered similarly by the print function.

When you run this script, you should see output similar to this:

uninit: {} true true
emp: {  } len: 3 cap: 3
set: {a b c}
get: c
len: 3
apd: {a b c d e f}
cpy: {a b c d e f}
sl1: {c d e}
sl2: {a b c d e}
sl3: {c d e f}
dcl: {g h i}
t == t2
2d: {{0} {1 2} {2 3 4}}

AngelScript doesn’t have a built-in slices type like Go, so we’re using the array type which is similar in many ways. Some key differences:

  1. AngelScript arrays are zero-indexed, like Go slices.
  2. The append function in Go is replaced by the insertLast method in AngelScript.
  3. Slicing in AngelScript is done with the slice method, rather than the [:] syntax in Go.
  4. Capacity in AngelScript is handled automatically and can be queried with the capacity() method.
  5. Multi-dimensional arrays in AngelScript are created as arrays of arrays.

Remember that AngelScript’s array handling may not be as optimized for these operations as Go’s slice implementation, so performance characteristics might differ.