Skip to content

The Class Model

A class groups fields and methods into a value you can pass around. It is useful for a musical object whose behavior needs several related values: a motif builder, a configuration, or a script pattern.

Calling the class runs its constructor and creates an instance. Assigning that instance to another name shares it; calling a method that changes this changes the instance both names see. This lets a helper configure the object its caller will use.

class Phrase {
let root;
fn new(root) { this.root = root; }
fn transpose(amount) { this.root = this.root + amount; }
fn notes() { return #[this.root, this.root + 7]; }
}
let phrase = Phrase(60);
let alias = phrase;
phrase.transpose(12);
assert alias.root == 72;
print(alias.notes());

Methods can instead return new objects to preserve the original. A class that implements Clone defines clone(), which gives an independent copy when shared mutation is not the intended behavior. The class decides what its copy is: the engine does not invent one for it. The difference is a choice made by the method, not something inferred from its name.

A field or function annotation is a contract, and it holds: every write to a typed field is checked, and so is each argument and return of an annotated function. When an instance is made, a typed field that cannot be NUL must be set by new. A class can declare a from conversion for values it deliberately accepts; otherwise a value must already fit the annotation. See Conversions.

Resonon classes have no inheritance, static members or private fields. Reuse comes from interfaces with default methods, composing values and calling helper functions; module-level private limits what a module exports.

An instance of a class that implements Pattern is a script pattern. Sending it to a track, or transforming it with a combinator such as .fast(), captures its fields for playback. Changing the original instance later does not change that playing snapshot; send the instance again to replace the pattern. This separates editing an object from launching its musical result at a cycle boundary.

Classes specifies the syntax; Script Patterns specifies the query contract.