Classes
Syntax
Classes are a kind of struct that support more advanced features.
class Node {
Node next;
int value;
Node(int val) {
this.value = val;
}
}
Usage
Classes are pass-by-reference and are always instantiated on the heap by internally using amethyst:gc/malloc. In most cases, manually allocating memory on the heap is unecessary as classes are instantiated with the new keyword like so:
var start = new Node(5);
var end = new Node(10);
start.next = end;
Amethyst internally uses immutable references to access classes on the heap. This allows easy control over changing which reference points to which object.
Differences from Structs
- Classes can hold any type of property, including references and other classes.
- Classes are pass-by-reference.
- Classes have a hidden
@typeproperty that holds the ID of the class. - Constructors have a hidden
thisparameter just like methods.
Garbage Collection
This feature is currently WIP.
Objects allocated on the heap will be deleted if no surviving references to it are found. Every few ticks, the garbage collector will run automatically.
Places references are checked for:
- Global variables
- Lists if strongly-typed
- Class properties
Blocked locations for references:
- Struct properties
- Entity properties
- Maps
To get around these limitations, use weak references.
Storing lists or classes as nbt or nbt& will cause nested references to not be correctly marked as alive. Additionally, references to references do not check nested properties.