Skip to content

Split a Piece into Modules

Use local modules to share definitions without repeating them in the main piece.

Create src/patterns.non beside src/main.non and save:

fn kick() {
return [bd _ bd _];
}

Keep track creation in the main piece for this example. In src/main.non, save:

use "./patterns" as grooves;
use "std/instruments" { Sampler, Kit };
let drums = AudioTrack("drums");
drums.set_instrument(Sampler(Kit("CR-78")));
drums << grooves.kick();
PLAY;

Evaluate the main file in VSCode. The relative import is resolved from the importing file, so both files can move together with the project.

Change the returned pattern in patterns.non and save it. Re-evaluate the use and the line that sends grooves.kick() to the track. The edited module is evaluated again, and the new pattern is sent to the existing track. Check the change at the next cycle boundary.

Use a module alias when helpers in different files have the same name. For helpers used only inside the module, add private before their definitions. If two modules import each other, move the shared definitions into a third file and import it from both.

Modules lists the import forms; Module Identity explains reuse and reloads.